[英]Odd javascript behavior for checking “constructor” key in object
I am actually not sure if I just stumbled upon an unwanted behavior in javascript or if this is somehow intended behavior. 我实际上不确定我是不是偶然发现了javascript中的不需要的行为,或者这是否是某种预期的行为。
The following code results in a true statement: 以下代码生成一个真实的语句:
var test= {"test":1}
document.write("constructor" in test);
http://jsfiddle.net/xyatxm2g/2/ http://jsfiddle.net/xyatxm2g/2/
If I change it to the following code, it returns false as it should: 如果我将其更改为以下代码,它将返回false,因为它应该:
var test= {"test":1}
document.write(test.hasOwnProperty("constructor"));
http://jsfiddle.net/fg06ovvc/2/ http://jsfiddle.net/fg06ovvc/2/
The hasOwnProperty
method , as the name says, look into the object to see if it has the property itself. 正如名称所示,
hasOwnProperty
方法查看对象以查看它是否具有该属性本身。
But when you use 'propertyName' in test
, you're not only looking into the object's own properties, but also the properties that come from inheritance. 但是当你
'propertyName' in test
使用'propertyName' in test
,你不仅要查看对象自己的属性,还要查看继承中的属性。
In that case, constructor
is a property that resides inside the Object
's prototype , so all objects have that property, because they all inherit from Object
. 在这种情况下,
constructor
是驻留在Object
原型中的属性,因此所有对象都具有该属性,因为它们都从Object
继承。
Quote from MDN 来自MDN的报价
Every object descended from Object inherits the hasOwnProperty method.
来自Object的每个对象都继承hasOwnProperty方法。 This method can be used to determine whether an object has the specified property as a direct property of that object;
此方法可用于确定对象是否具有指定的属性作为该对象的直接属性; unlike the
in
operator , this method does not check down the object's prototype chain.与
in
运算符不同,此方法不会检查对象的原型链。
From the MDN documentation : 从MDN文档 :
Inherited properties
继承的属性
The in operator returns true for properties in the prototype chain.in运算符为原型链中的属性返回true。
"toString" in {}; // returns true
Whereas the hasOwnProperty()
method only checks for properties directly on the object, not inherited (ie not on the prototype chain). 而
hasOwnProperty()
方法只检查对象上的属性,而不是继承(即不在原型链上)。
Following MDN documentation, it is not an enumerable field. 在MDN文档之后,它不是可枚举的字段。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
You can perform following test: 您可以执行以下测试:
var obj = {"t": 23};
obj.propertyIsEnumerable("t")
results: true 结果:是的
obj.propertyIsEnumerable("constructor")
results: false
结果:
false
There is a complete example on this document, under section: 本文档的完整示例见以下部分:
Direct versus inherited properties
直接与继承属性
I think it may be normal behaviour here that the key in object
operator is searching through the prototype chain and returning true for Object.prototype.constructor
. 我认为这里的正常行为
key in object
操作符中的key in object
正在搜索原型链并为Object.prototype.constructor
返回true。 See this discussion - it goes over a related topic. 请参阅此讨论 - 它涉及相关主题。
How do I check if an object has a property in JavaScript? 如何检查对象是否在JavaScript中具有属性?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.