for-in循环VS in-operator [英] for-in loop VS in-operator
问题描述
我认为自己是JS老手,但是刚刚第一次意识到在循环中的与
in
运算符:
(true)
for(k in []){if(k ==length)alert(); }; // k永远不会是长度
所以这引出了我的问题:为什么是 for ... in
循环中运算符中的
使得JS引擎获取所有可枚举的属性,然后在该子集上操作符中的
使用的是imho错误:仅仅是因为操作符中的
接受了所有可能的属性,无论是原型还是自己的属性,无论是访问器属性还是数据属性。那么如果已经减少到可枚举对象的属性,那么如何过滤掉所有的东西呢?
所以这给我带来了我的问题:为什么
运算符中的
都存在于
中...
循环?
不是。 for ... in
循环结构中的标记中的
标记不是一个运算符,它同样不是一个运算符表达
x = {in:5} .in
这只是其中一个令牌,可以区分 从 I consider myself a JS veteran but just now for the first time I have realised that the So this brings me to my question: why is the Imho it is totally misleading, as it does different things. Also the notion that first the So this brings me to my question: why is the It isn't. The It's just one of the tokens that distinguishes a 这篇关于for-in循环VS in-operator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! www.ecma-international.org/ecma-262/6.0/#sec-for-statementrel =nofollow>
语句不存在 for(...; ...; ...)
一个>循环。鉴于(...),在标记中的
http://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statementsrel =nofollow>关系表达式
永远不会与此冲突。 for ... in
loop does something very different from the in
operator:"length" in []; // true
for (k in []) { if(k == "length") alert(); }; // k will never be "length"
in
operator at all present in the for ... in
loop?for
operation makes the JS engine take all enumerable properties and then on that subset the in
operator is used is imho wrong: simply because the in
operator accepts all possible properties whether prototype or own, whether accessor- or data-properties. So how does it filter anything out if for
has already reduced to enumerable object properties?
in
operator at all present in the for ... in
loop?in
token in the for … in
loop construct is not an operator, just as much it is not one in the expressionx = {in: 5}.in
for ( … in … )
from a for (…; …; …)
loop. Given that there is no for (…)
statement, the role of the in
token in a relational expression never collides with this.