Two methods, but slightly different
1, in operator
var obj = {name:'jack'}; alert('name' in obj); // --> true alert('toString' in obj); // --> trueYou can see that whether it is name or toString on the original chain, it can detect the return true.
2. HasOwnProperty method
var obj = {name:'jack'}; obj.hasOwnProperty('name'); // --> true obj.hasOwnProperty('toString'); // --> falseThe inherited properties on the prototype chain cannot be detected by hasOwnProperty, returning false.
It should be noted that although in can detect the properties of the prototype chain, for in usually does not work.
Of course, after rewriting the prototype for in is visible under IE9/Firefox/Safari/Chrome/Opera.
The above method to determine whether a JS object has a certain attribute is the entire content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.