请解释一下这个奇怪的 JavaScript 行
我遇到了一段代码:
for(i=((90.0E1,0x5A)<=(0x158,140.70E1)?(.28,3.45E2,0):(95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227));i<length;i++) {
// some other code here
}
有人可以帮我解释一下 for() 括号中的内容吗?
I came across a piece of code that is:
for(i=((90.0E1,0x5A)<=(0x158,140.70E1)?(.28,3.45E2,0):(95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227));i<length;i++) {
// some other code here
}
Can someone help me by explaining the stuff in the for() brackets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
逗号运算符的结果始终是右手边。
因此 (a,b) 形式的每一对都计算为 b。由于在您的代码中“a”永远不会产生副作用,因此我们可以
只需省略它即可:
其中“...”代表无关紧要的东西:
由于 0x5A <= 140.70E1 计算结果为
true
,因此 ?: 运算符是问号右侧的值,即 0。所以结果相当于
which 有意义。
The result of the comma operator is always the value to the right hand side.
So each pair of the form (a,b) evaluates to b. Since in your code "a" never has a side effect, we can
simply omit it to get:
Where "..." stands for stuff that doesn't matter:
Since 0x5A <= 140.70E1 evaluates to
true
, the result of the ?: operator is the value to the right of the question mark, i.e. 0.So the result is equivalent to
which makes sense.
它是一个标准的三表达式
for
语句,其中第一个表达式(初始化器)恰好被定义为在此表达式中,三元
?:
运算符,并且,让事情变得复杂的是,以嵌套方式执行此操作。?:
运算符的语法如下鉴于此,表达式由以下内容组成
value-if-false 使用
?:
运算符保存嵌套表达式,其中当然可以用同样的方式解构。It is a standard three-expression
for
statement, where the first expression, the initializer, happens to be defined asIn this expression, the ternary
?:
operator, and, to complicate things, does this in a nested fashion.The syntax of the
?:
operator is the followingGiven this, the expression is composed of the following
The value-if-false holds a nested expression using the
?:
operator which can of course be deconstructed in the same way.简化十六进制和 E 数字,它变成:
这使得代码等效于:
这是一种非常有创意且令人困惑的
for
循环方式,也是一个很好的笑话。Simplifying the hex and E numbers, it becomes:
which makes the code equivalent to:
It's a very creative and confusing way to make a
for
loop, and a good joke.