为什么在定义一系列javascript原型函数(类方法)时使用逗号?

时间:2015-12-08 08:05:59

标签: javascript firefox-addon

我是js的新手,我从here

中读取了一些代码
function ChaZD(a, b, c, d) {
    this.wordSource = c, this.useHttps = b;
    var e = (b ? urls.dictHttps : urls.dict) + a,
        f = this,
        g = new XMLHttpRequest;
    g.open("GET", e, !0), g.onreadystatechange = function() {
        if (4 == g.readyState) {
            var e = JSON.parse(g.responseText);
            if (-1 === a.indexOf("-") || f.checkErrorCode(e.errorCode).error || f.haveTranslation(e)) {
                var h = f.parseResult.call(f, e);
                d(h)
            } else new ChaZD(a.replace(/-/g, " "), b, c, d)
        }
    }, g.send()
}

ChaZD.prototype.checkErrorCode = function(a) {
    var b = {
        message: "",
        error: 0,
        errorCode: 0
    };
    switch (a) {
        case 0:
            b.message = "query success";
            break;
        case 20:
            b.message = "要翻译的文本过长", b.error = 1, b.errorCode = 20;
            break;
        case 30:
            b.message = "无法进行有效的翻译", b.error = 1, b.errorCode = 30;
            break;
        case 40:
            b.message = "不支持的语言类型", b.error = 1, b.errorCode = 40;
            break;
        case 50:
            b.message = "无效的key", b.error = 1, b.errorCode = 50;
            break;
        case 60:
            b.message = "无辞典结果", b.error = 1, b.errorCode = 60
    }
    return b

// why a comma here ?

}, ChaZD.prototype.parseResult = function(a) {
    var b = {},
        c = this.checkErrorCode(a.errorCode);
    if (b.haveWebTranslation = !1, c.error) b.errorCode = c.errorCode;
    else {
        var d = this.initTitle(a);
        if (b.titleBlock = d.titleBlock, b.haveTranslation = this.haveTranslation(a), void 0 !== a.basic) {
            var e = this.parseBasicResult(a);
            b.basicBlock = e
        }
        if (void 0 !== a.web) {
            var f = this.parseWebResult(a);
            b.haveWebTranslation = !0, b.webBlock = f
        }
    }
    return b.validMessage = c.message, b

// why a comma here ?

}, ChaZD.prototype.haveTranslation = function(a) {
    if (this.checkErrorCode(a.errorCode).error) return !1;
    var b = a.translation,
        c = a.query;
    return trim(c.toLowerCase()) === trim(b.toString().toLowerCase()) ? !1 : !0
}, ..... another class method definition here
...
and a lot more other class method definitions

定义ChaZD的类方法时,为什么每个方法定义后都有逗号?为什么不把它们分开呢?比如

function ChaZD(a,b,c,d){
    // some code
}

ChaZD.prototype.checkErrorCode = function(a) {
    // some code
}

ChaZD.prototype.parseResult = function(a) {
    // some code
}

ChaZD.prototype.anotherFunction = function(a) {
    // some code
}
...

虽然现在就像

function ChaZD(a,b,c,d){
    // some code
}

ChaZD.prototype.checkErrorCode = function(a) {
    // some code
}, ChaZD.prototype.parseResult = function(a) {
    // some code
}, ChaZD.prototype.anotherFunction = function(a) {
    // some code
}, ...

使用逗号分隔每个类方法定义有什么好处吗?

2 个答案:

答案 0 :(得分:1)

使用逗号和分隔之间没有区别,可能作者认为这更好地组织了。

它的写作等同于

var a = 50, b = 20, c = 25;

Vs的

var a = 50;
var b = 20;
var c = 25;

答案 1 :(得分:1)

这就像初始化变量一样,只是该值是对匿名函数的引用

var a  = 1;
var b = 2;

var a = 1, b =2;

类似地

var a  = function(){};
var b = function(){};

var a = function(){}, b =function(){};

或特别针对您的情况

function ChaZD(){..}
ChaZD.prototype.func1 = function(){};
ChaZD.prototype.func2 = function(){};

ChaZD.prototype.func1 = function(){}, ChaZD.prototype.func2 = function(){};