Two ways, but slightly different
1. in operator
Copy the code code as follows:
var obj = {name:'jack'};
alert('name' in obj); // --> true
alert('toString' in obj); // --> true
It can be seen that both name and toString on the prototype chain can be detected and return true.
2. hasOwnProperty method
Copy the code code as follows:
var obj = {name:'jack'};
obj.hasOwnProperty('name'); // --> true
obj.hasOwnProperty('toString'); // --> false
Properties inherited on the prototype chain cannot be detected through hasOwnProperty, and false is returned.
It should be noted that although in can detect the properties of the prototype chain, for in usually cannot.
Of course, after rewriting the prototype, for in will be visible under IE9/Firefox/Safari/Chrome/Opera. See: Pitfalls of for in