5

I am actually not sure if I just stumbled upon an unwanted behavior in javascript or if this is somehow intended behavior.

The following code results in a true statement:

var test= {"test":1}
document.write("constructor" in test);    

http://jsfiddle.net/xyatxm2g/2/

If I change it to the following code, it returns false as it should:

var test= {"test":1}
document.write(test.hasOwnProperty("constructor"));

http://jsfiddle.net/fg06ovvc/2/

fsociety
  • 1,791
  • 4
  • 22
  • 32

4 Answers4

9

The hasOwnProperty method, as the name says, look into the object to see if it has the property itself.

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.

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.

Quote from MDN

Every object descended from Object inherits the hasOwnProperty method. 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.

Buzinas
  • 11,597
  • 2
  • 36
  • 58
  • So if I want to check for a key in a dictionary, I should always prefer to use the ```hasOwnProperty``` method? – fsociety Oct 01 '15 at 00:05
  • @barsch Yes. That's why to iterate over all the actual keys of an object (if `Object.keys(obj)` is not available), you do `for (var key in obj) { if (obj.hasOwnProperty(key)) { doSomethingWithKey(key); } }`. – GregL Oct 01 '15 at 00:07
  • @barsch If you're only targeting new browsers, you can also use [the Map type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) instead. In the future, using standard JavaScript Objects as maps will probably become less popular now that we have a more specialized option. – Jeremy Oct 01 '15 at 00:38
2

From the MDN documentation:

Inherited properties
The in operator returns true for properties in the prototype chain.
"toString" in {}; // returns true

Whereas the hasOwnProperty() method only checks for properties directly on the object, not inherited (i.e. not on the prototype chain).

GregL
  • 37,147
  • 8
  • 62
  • 67
1

Following MDN documentation, it is not an enumerable field.

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

There is a complete example on this document, under section:

Direct versus inherited properties

Andre Pastore
  • 2,841
  • 4
  • 33
  • 44
1

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. See this discussion - it goes over a related topic.

How do I check if an object has a property in JavaScript?

Community
  • 1
  • 1
3066d0
  • 1,853
  • 1
  • 14
  • 18