Javascript for..in 和 hasOwnProperty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12735778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
for..in and hasOwnProperty
提问by scusyxx
Possible Duplicate:
How do I check to see if an object has a property in Javascript?
I found the following snippet in Twitter's JS files. I was wondering why do they need to call the hasOwnProperty
function to see dict
has the key
property? The for loop is running for each 'key' in 'dict' which means 'dict' has 'key', am I missing a point?
我在 Twitter 的 JS 文件中发现了以下代码段。我想知道为什么他们需要调用该hasOwnProperty
函数才能看到dict
该key
属性?for 循环为“dict”中的每个“key”运行,这意味着“dict”有“key”,我错过了一个点吗?
function forEach(dict, f) {
for (key in dict) {
if (dict.hasOwnProperty(key))
f(key, dict[key]);
}
}
回答by blockhead
Because if you don't, it will loop through every property on the prototype chain, including ones that you don't know about (that were possibly added by somebody messing with native object prototypes).
因为如果你不这样做,它会遍历原型链上的每个属性,包括你不知道的那些(可能是由一些弄乱本机对象原型的人添加的)。
This way you're guaranteed only the keys that are on that object instance itself.
通过这种方式,您只能保证该对象实例本身上的键。
回答by Paul S.
The hasOwnPropertymethod lets you know if a property is directly on an instance of an object or inherited from it's prototype chain.
该hasOwnProperty方法可以让你知道,如果一个属性是直接在对象的实例或它的原型链继承。
Consider the following
考虑以下
function ObjWithProto() {
this.foo = 'foo_val';
}
ObjWithProto.prototype = {bar: 'bar_val'};
var dict = new ObjWithProto();
dict.foobar = 'foobar_val';
i.e. you have an Objectdict
with properties foo
and foobar
that also inherits a property bar
from it's prototype chain.
即你有一个对象dict
具有属性foo
和foobar
还继承了财产bar
从它的原型链。
Now run it through (a modified version of) your code
现在运行它(修改后的版本)你的代码
function forEach(dict) {
var key;
for (key in dict) {
if ( dict.hasOwnProperty(key) ) console.log('has', key, dict[key]);
else console.log('not', key, dict[key]);
}
}
forEach( dict );
You will see
你会看见
has foo foo_val
has foobar foobar_val
not bar bar_val
This lets you separate properties that an object has itself and those it has inherited (which are usually methods that aren't relevant to the loop)
这使您可以分离对象自身的属性和它继承的属性(通常是与循环无关的方法)
Furthermore, if you now do dict.bar = 'new_bar_val';
, the last result will change to has bar new_bar_val
, letting you distinguish even between properties of the same name as those inherited.
此外,如果您现在这样做dict.bar = 'new_bar_val';
,最后的结果将更改为has bar new_bar_val
,让您甚至可以区分与继承的属性同名的属性。
回答by A. Matías Quezada
Every object on javascript is a dictionary, this means that "toString" and every other method is a key of every Object
javascript 上的每个对象都是一个字典,这意味着“toString”和其他所有方法都是每个对象的键
var myObj = {};
console.log(myObj["toString"]);
But this function is inherited from Object class, so hasOwnProperty tells you if this key is owned by the dictionary or if it is inherited.
但是这个函数是从Object类继承的,所以hasOwnProperty会告诉你这个键是属于字典的还是被继承的。
"toString" in myObj; // true
myObj.hasOwnProperty("toString") // false
回答by d1pr3d
@blockhead is right here. For example, Prototype.js framework used to extend native arrays with extra helper methods (I do not know the situation with current versions of a framework). Thus straight usage of "for (key in dict)" would return all the elements of the div plus references to helper methods. Which is kind of unexpected :)
@blockhead 就在这里。例如,Prototype.js 框架用于使用额外的辅助方法扩展本机数组(我不知道当前版本的框架的情况)。因此,直接使用“for (key in dict)”将返回 div 的所有元素以及对辅助方法的引用。这有点出乎意料:)