jquery脚本仅适用于firefox


jquery script works only on firefox

在我的第一个简单网站/数据库上花了两周的时间之后,我被卡住了。我的一个朋友帮我添加了jquery,但现在它只在Mozilla中有效,他不知道为什么。我根本不懂java(几乎不懂php)。你能看看吗?

Chrome控制台指向错误

Uncaught SyntaxError: Unexpected token = 

在第49行,它是

self.dataAdapter = function(id = 0, imie = '', nazwisko = '', kredyt = 0)

你知道什么是最兼容的语法吗?

整个脚本:

$(document).ready(function()
{
    sui = new swiezakUI();
    sui.getData();
});
function swiezakUI() 
{
    var self = this;
    self.scriptURL = "/data.php";
    self.send = function(key, stuff)
    {
        var post = {};
        post[key] = JSON.stringify(stuff);
        $.ajax({
            type: "POST",
            url: self.scriptURL,
            data: post,
            contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            success: function(data)
            {
                self.getData();
                self.cleanForm();
            },
            failure: function(errMsg)
            {
                alert('fail');
            },
            error: function(errMsg)
            {
                alert("Blad 'n" + errMsg.responseText);
            }
        });     
    }
    self.id2Id = function(id)
    {
        for(var i = 0; i < self.myData.length; i++)
        {
            if(id == self.myData[i].id) 
                return i;
        }
    }
    self.dataAdapter = function(id = 0, imie = '', nazwisko = '', kredyt = 0)
    {
        var data = new Object();
        data.id = id;
        data.imie = imie;
        data.nazwisko = nazwisko;
        data.kredyt = kredyt;
        return data;
    }
    self.dodajNowy = function()
    {
        return function()
        {
            var data = self.dataAdapter(null, $('#imie').val(), $('#nazwisko').val(), $('#kredyt').val().replace(/'D+/g,""));
            self.send('nowy',data);
        }
    }
    self.edytujWpis = function(id)
    {
        return function()
        {
            var data = self.dataAdapter(id, $('#imie').val(), $('#nazwisko').val(), $('#kredyt').val().replace(/'D+/g,""));
            self.send('edycja',data);
        }
    }
    self.kasujWpis = function(id)
    {
        return function()
        {
            var data = self.dataAdapter(id);
            self.send('kasuj',data);
        }
    }
    self.cleanForm = function()
    {
        $('#imie').val('');
        $('#nazwisko').val('');
        $('#kredyt').val('');
        $('#bZapisz').unbind();
        $('#bZapisz').click(self.dodajNowy());
    }
    self.editButtons = function()
    {
        $('.edit').click(function()
        {   
            var did = $(this).attr('id').replace(/'D+/g,"");
            id = self.id2Id(did);
            $('#imie').val(self.myData[id].imie);
            $('#nazwisko').val(self.myData[id].nazwisko);
            $('#kredyt').val(self.myData[id].kredyt);
            $('#bZapisz').unbind();
            $('#bZapisz').click(self.edytujWpis(did));          
        });
    }
    self.delButtons = function()
    {
        $('.delete').click(function()
        {
            var id = $(this).attr('id').replace(/'D+/g,"");
            console.log(id);
            self.kasujWpis(id)();
        });
    }
    $('#bZapisz').click(self.dodajNowy());
    $('#bCzysc').click(function(){
        self.cleanForm();
    });
    self.getData = function()
    {
        $('#lista').children('table').html("<tr><th>id</th><th>imie</th><th>nazwisko</th><th>kredyt</th>"+ 
            "<th>edycja</th><th>usun</th></tr>");
        $.getJSON(self.scriptURL, function(data)
        {           
            console.log(data);
            self.myData = data;
            for(var i = 0; i < data.length; i++)
            {
                $('#lista').children('table').append(
                    '<tr><td>'+ data[i].id +'</td>'+
                    '<td>'+ data[i].imie +'</td>'+
                    '<td>'+ data[i].nazwisko +'</td>'+
                    '<td>'+ data[i].kredyt +'</td>'+
                    '<td><button class="edit" id="e#'+data[i].id+'">e</button></td>'+
                    '<td><button class="delete" id="d#'+data[i].id+'">d</button></td></tr>');
            }
            self.editButtons();
            self.delButtons();
        });
    }
}

默认参数目前是ES6草案的一部分。此功能不是最新的ECMAScript最终标准(5.1)的一部分,因此浏览器支持目前很少。截至本文撰写之时,只有Firefox(实验性地)实现默认参数。

有许多方法可以模拟默认参数。ES6默认参数的替代方案是比较arg === undefined,为其设置默认值:

//if the kredyt argument has not been passed or is undefined
if (kredyt === undefined) {
   kredyt = 0;
}

当调用传递的参数少于其参数计数的函数时,没有相应参数的参数将初始化为undefined

这样,不为参数传递值以及显式传递undefined作为参数值将使用默认参数值,这与ES6的默认参数的行为相同。

下面是一个完整的例子:

self.dataAdapter = function(id, imie, nazwisko, kredyt)
{
    //default parameters
    if (id === undefined) {
        id = 0;
    }
    if (imie === undefined) {
        imie = '';
    }
    if (nazwisko === undefined) {
        nazwisko = '';
    }
    if (kredyt === undefined) {
        kredyt = 0;
    }
    //function code
    var data = new Object();
    data.id = id;
    data.imie = imie;
    data.nazwisko = nazwisko;
    data.kredyt = kredyt;
    return data;
}

另一种常见的方法是比较arg == null,它具有相同的效果,但也接受传递null以使用默认参数值:

//if kredyt is null, undefined or not passed
if (kredyt == null) {
   kredyt = 0;
}

这是因为==进行类型强制,而null强制到并且仅强制到undefined(ES5#11.9.3)。


另一种常见的方法是使用||作为"默认运算符"(参见相关问题):

kredyt = kredyt || 0; //in the beginning of the function
//...
data.kredyt = kredyt;

或者,如果参数只在一个地方使用,也可以将其内联:

data.kredyt = kredyt || 0;

好处基本上是代码更短,但请注意,它不仅在参数为undefined时,而且在nullfalse0、空字符串或NaN时都会使用默认参数值。因此,当其中一个值是与其默认参数值不同的可接受参数值时,这不是一种可行的方法。


最后,对于需要将null/undefined值与未传递的参数区分开来的用例(这种情况很少见,这里的情况并非如此),可以检查arguments对象的length属性:

if (arguments.length < 4) {
    kredyt = 0;
}