如何知道逗号是分隔参数还是箭头函数的一部分?
How to know if comma separates params or is part of arrow function?
我一直在胡思乱想在短路操作中使用逗号:
let component = { on: (p1, p2, p3) => console.log(p1, p2, p3) };
component.on('something', () => console.log('what do'), '???');
'???'
是不是参数好像有点模糊
这有什么规定吗?
谢谢!
let canTry = true;
while (canTry --> false) {
console.log(('leet hax','your mom'));
}
order of operations 似乎没有帮助,因为它没有描述参数
规范定义了运算符的优先级,这就是你可以知道的。
<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-Expression" rel="noreferrer">Expression</a></i><sub>[In, Yield]</sub>:
<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-AssignmentExpression" rel="noreferrer">AssignmentExpression</a></i><sub>[?In, ?Yield]</sub>
<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-Expression" rel="noreferrer">Expression</a></i><sub>[?In, ?Yield]</sub> <b>,</b> <i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-AssignmentExpression" rel="noreferrer">AssignmentExpression</a></i><sub>[?In, ?Yield]</sub>
14.2 - Arrow Function Definitions
<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-ConciseBody" rel="noreferrer">ConciseBody</a></i><sub>[In]</sub>:
[lookahead ≠ <b>{</b>]<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-AssignmentExpression" rel="noreferrer">AssignmentExpression</a></i><sub>[?In]</sub>
<b>{</b><i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-FunctionBody" rel="noreferrer">FunctionBody</a></i><b>}</b>
箭头函数的简洁体必须是AssignmentExpression,不能直接包含逗号。但是逗号运算符可以分隔不同的AssignmentExpressions.
如果你想说清楚你在做什么,加括号:
console.log( (() => 2), 3 );
console.log( (() => 2, 3) );
console.log( () => (2, 3) );
我一直在胡思乱想在短路操作中使用逗号:
let component = { on: (p1, p2, p3) => console.log(p1, p2, p3) };
component.on('something', () => console.log('what do'), '???');
'???'
是不是参数好像有点模糊
这有什么规定吗?
谢谢!
let canTry = true;
while (canTry --> false) {
console.log(('leet hax','your mom'));
}
order of operations 似乎没有帮助,因为它没有描述参数
规范定义了运算符的优先级,这就是你可以知道的。
<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-Expression" rel="noreferrer">Expression</a></i><sub>[In, Yield]</sub>: <i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-AssignmentExpression" rel="noreferrer">AssignmentExpression</a></i><sub>[?In, ?Yield]</sub> <i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-Expression" rel="noreferrer">Expression</a></i><sub>[?In, ?Yield]</sub> <b>,</b> <i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-AssignmentExpression" rel="noreferrer">AssignmentExpression</a></i><sub>[?In, ?Yield]</sub>
14.2 - Arrow Function Definitions
<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-ConciseBody" rel="noreferrer">ConciseBody</a></i><sub>[In]</sub>: [lookahead ≠ <b>{</b>]<i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-AssignmentExpression" rel="noreferrer">AssignmentExpression</a></i><sub>[?In]</sub> <b>{</b><i><a href="http://www.ecma-international.org/ecma-262/7.0/#prod-FunctionBody" rel="noreferrer">FunctionBody</a></i><b>}</b>
箭头函数的简洁体必须是AssignmentExpression,不能直接包含逗号。但是逗号运算符可以分隔不同的AssignmentExpressions.
如果你想说清楚你在做什么,加括号:
console.log( (() => 2), 3 );
console.log( (() => 2, 3) );
console.log( () => (2, 3) );