我是 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
}, ...
使用逗号分隔每个类方法定义有什么好处吗?
最佳答案
使用逗号和分隔没有区别,可能是作者认为这样更有条理。
相当于写作
var a = 50, b = 20, c = 25;
对比
var a = 50;
var b = 20;
var c = 25;
关于javascript - 为什么在定义一系列javascript原型(prototype)函数(类方法)时使用逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34150851/