gpt4 book ai didi

javascript - javascript中&&的运算符优先级/执行顺序是什么

转载 作者:行者123 更新时间:2023-11-29 17:45:42 25 4
gpt4 key购买 nike

javascript 中 && 的运算符优先级/执行顺序是什么

str = "string"
iterator = str[Symbol.iterator]()
while ( result = iterator.next() && !result.done ) { // Error result is not defined..Is it not a left to right evaluatation
console.log( result.value )
}

通过添加括号来更改它似乎可以解决问题......

str = "string"
iterator = str[Symbol.iterator]()
while ( ( result = iterator.next() ) && !result.done ) { // Parenthesis seem to take precedence
console.log( result.value )
}

最佳答案

嗯,因为你有赋值,所以它被执行为:

result = (iterator.next() && !result.done)

代替

(result = iterator.next()) && !result.done

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

关于javascript - javascript中&&的运算符优先级/执行顺序是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50001422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com