This article mainly describes how to determine whether a JS object has a certain attribute. If you need it, you can refer to whether a JS object has a certain attribute.
Two methods, but slightly different
1, in Operator
Var obj = {name: 'jack'}; alert ('name' in obj); // --> truealert ('tostring' in obj); // --> true
We can see that both the name and the toString on the prototype chain can detect that the return value is true.
2. hasOwnProperty Method
Var obj = {name: 'jack'}; obj. hasOwnProperty ('name'); // --> trueobj. hasOwnProperty ('tostring'); // --> false
Attributes inherited from the prototype chain cannot be detected through hasOwnProperty. false is returned.
It should be noted that, although in can detect the attributes of the prototype chain, for in usually does not work.
Of course, after the prototype is rewritten, for in is visible under IE9/Firefox/Safari/Chrome/Opera. See: for in defects
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!
For more information about how to determine whether a JS object has a certain attribute, refer to PHP!