[英]Javascript syntax need some explanation
I am reading Javascript the Good Parts and came across the following snippet under Chapter 5 Inheritance: 我正在阅读Javascript的好部分,并在第5章继承下遇到了以下代码段:
var coolcat = function (spec) {
var that = cat(spec),
super_get_name = that.superior('get_name');
that.get_name = function (n) {
return 'like ' + super_get_name() + ' baby'; return that;
}
}
I am confused by the coma after cat(spec) in line 2. What does the line do exactly? 我对第2行cat(spec)之后的昏迷感到困惑。 (line 2 +line 3) Thanks
(第2行+第3行)谢谢
That's just a shortcut for declaring two variables in one statement, it is equivalent to this: 这只是在一条语句中声明两个变量的快捷方式,它等效于此:
var that = cat(spec);
var super_get_name = that.superior('get_name');
The comma is actually an operator in JavaScript: 逗号实际上是 JavaScript中的运算符 :
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.
逗号运算符计算两个操作数(从左到右)并返回第二个操作数的值。
A var
statement is made up of one or more expressions of the form: var
语句由以下形式的一个或多个表达式组成:
varname [= value]
where the square brackets indicate an optional component. 其中方括号表示可选组件。 The general
var
statement looks like this: 一般的
var
语句如下所示:
var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];
var varname1 [= value1 [,varname2 [,varname3 ... [,varnameN]]]];
You'll usually only see the comma operator used in var
statements and for
loops : 通常,您只会看到
var
语句和for
循环中使用的逗号运算符:
for(var i = 0, x = complicated_array[0]; i < complicated_array.length; x = complicated_array[++i])
but it can be used in other places. 但可以在其他地方使用。
It lets you declare another variable. 它使您可以声明另一个变量。 It's equivalent to the following:
等效于以下内容:
var that = cat(spec);
var super_get_name = that.superior('get_name');
See the var
statement docs @ MDC . 请参阅
var
声明docs @ MDC 。
The indentation is wrong, it should be: 缩进是错误的,应该是:
var that = cat(spec),
super_get_name = that.superior('get_name');
It is the same as saying: 就像在说:
var that = cat(spec);
var super_get_name = that.superior('get_name');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.