请考虑以下内容:(
编辑:我稍微修改了函数以删除三元运算符的使用或括号)
function someFunction(start,end,step){
var start = start || 1,
end = end || 100,
boolEndBigger = (start < end); // define Boolean here
step = step || boolEndBigger ? 1:-1;
console.log(step);
}
someFunction()
// step isn't defined so expect (1<10) ? 1:-1 to evaluate to 1
someFunction(1,10)
// again step isn't defined so expect to log 1 as before
Run Code Online (Sandbox Code Playgroud)
问题:
Run Code Online (Sandbox Code Playgroud)someFunction(1,10,2) //step IS defined, shortcut logical OR || should kick in, //step should return 2 BUT it returns 1 …