There are two ways to determine whether a JS object has a property


Two ways, but slightly different

1, in operator

var obj = {name:'jack'};
alert('name' in obj); // --> true
alert('toString' in obj); // --> true

You can see that both the name and the toString on the primitive chain return true.

2. HasOwnProperty method

var obj = {name:'jack'};
obj.hasOwnProperty('name'); // --> true
obj.hasOwnProperty('toString'); // --> false

Properties inherited from the prototype chain cannot be detected by hasOwnProperty and return false.

Note that while in can detect properties of the prototype chain, for in usually does not.

Of course after rewriting the prototype for the in ie 9 / Firefox/Safari/Chrome/Opera is visible. See: (link: #)