了解 JavaScript 逗号运算符
Understanding the JavaScript comma operator
This article by Angus Croll 像这样解释 JavaScript 逗号运算符:
//(LHE: left hand expression, RHE right hand expression)
LHE && RHE
1. Always evaluate LHE
2. If LHE is true, evaluate RHE
LHE || RHE
1. Always evaluate LHE
2. If LHE is false, evaluate RHE
LHE, RHE
1. Always evaluate LHE
2. Always evaluate RHE
但是,我用下面的代码做了一个 jsfiddle 测试 enter link description here,如果运算符是 &&
,LHE 似乎必须用括号括起来。
// Works fine
(function one(window) {
window.testOne = function testOne() {
alert("Test one");
}, testOne();
})(window);
// Works, but JSHint complains at *:
// "Expected an assignment or function call but saw instead an expression"
(function two(window) {
(window.testTwo = function testTwo() {
alert("Test two");
}) && testTwo(); // *
})(window);
// Looks good to JSHint, but fails at runtime:
// "ReferenceError: Can't find variable: testThree"
(function three(window) {
window.testThree = function testThree() {
alert("Test three");
} && testThree();
})(window);
您能解释一下为什么 testOne
(使用 ,
)不需要在第一个表达式周围加括号,而 testTwo
(使用 &&
)需要吗?为什么 JSHint 认为 test()
不是函数调用?
这是一个运算符优先级的例子。您使用的运算符具有以下优先级:&&
、||
、=
、,
。
这意味着 var ... = ... && ...
等同于 var ... = (... && ...)
但 var ... = ... , ....
等同于 (var ... = ...) , ....
。
例如,您可以检查优先级 here。
这段代码先赋值再调用
(window.testTwo = function testTwo() {
alert("Test two");
}) && testTwo();
- 赋值
window.testTwo = function testTwo() { alert("Test two") };
- 致电
testTwo()
但是另一个尝试在赋值之前调用
window.testThree = function testThree() {
alert("Test three");
} && testThree();
- 计算函数表达式(不是声明,所以没有创建
testThree
变量!)function testThree() { alert("Test three") }
- 调用并分配
window.testThree = testThree();
但是,testThree
未申报。所以抛出一个错误。
This article by Angus Croll 像这样解释 JavaScript 逗号运算符:
//(LHE: left hand expression, RHE right hand expression)
LHE && RHE
1. Always evaluate LHE
2. If LHE is true, evaluate RHE
LHE || RHE
1. Always evaluate LHE
2. If LHE is false, evaluate RHE
LHE, RHE
1. Always evaluate LHE
2. Always evaluate RHE
但是,我用下面的代码做了一个 jsfiddle 测试 enter link description here,如果运算符是 &&
,LHE 似乎必须用括号括起来。
// Works fine
(function one(window) {
window.testOne = function testOne() {
alert("Test one");
}, testOne();
})(window);
// Works, but JSHint complains at *:
// "Expected an assignment or function call but saw instead an expression"
(function two(window) {
(window.testTwo = function testTwo() {
alert("Test two");
}) && testTwo(); // *
})(window);
// Looks good to JSHint, but fails at runtime:
// "ReferenceError: Can't find variable: testThree"
(function three(window) {
window.testThree = function testThree() {
alert("Test three");
} && testThree();
})(window);
您能解释一下为什么 testOne
(使用 ,
)不需要在第一个表达式周围加括号,而 testTwo
(使用 &&
)需要吗?为什么 JSHint 认为 test()
不是函数调用?
这是一个运算符优先级的例子。您使用的运算符具有以下优先级:&&
、||
、=
、,
。
这意味着 var ... = ... && ...
等同于 var ... = (... && ...)
但 var ... = ... , ....
等同于 (var ... = ...) , ....
。
例如,您可以检查优先级 here。
这段代码先赋值再调用
(window.testTwo = function testTwo() {
alert("Test two");
}) && testTwo();
- 赋值
window.testTwo = function testTwo() { alert("Test two") };
- 致电
testTwo()
但是另一个尝试在赋值之前调用
window.testThree = function testThree() {
alert("Test three");
} && testThree();
- 计算函数表达式(不是声明,所以没有创建
testThree
变量!)function testThree() { alert("Test three") }
- 调用并分配
window.testThree = testThree();
但是,testThree
未申报。所以抛出一个错误。