In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "es6 how to judge whether an object has a certain attribute", so the editor summarizes the following content to you, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "es6 how to judge whether an object has a certain attribute" article.
Judgment method: 1, use the "object. Attribute name! = = undefined" statement to judge, if the return value is true, then there is a certain property on the object; 2, use the "'attribute name' in object" statement, if you return true, there is a certain property; 3, use the "object .hasOwnProperty ('property name')" statement.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Es6 determines whether an object has an attribute.
Method 1: point (. ) + undefined judgment
We know that you can get the property value of an object through dots or square brackets, and return undefined if the property does not exist on the object. In this way, you can determine the own and inherited properties of the specified object, and if the object itself does not have a detected property, and there is one on the prototype chain, the property value on the prototype chain will be returned.
/ / create object let obj = {name: 'Scarlett', age: 37} console.log (obj.name! = = undefined) / / true own attribute exists console.log (obj.gender! = = undefined) / / false gender attribute does not exist / / add an enumerable attribute Object.prototype.nationality =' America'// on the prototype add an enumerable attribute Object.defineProperty (obj, 'occupation') on the obj object {value: 'actress', enumerable: false})
Simply, we can determine whether there is an attribute on the object by the return value of Object.propertyName! = = undefined. However, there is a situation in which the desired result cannot be returned if the property name exists and the attribute value is undefined.
/ / add an attribute obj.birthday = undefinedconsole.log (obj.birthday! = = undefined) / / false with a value of undefined
Then, we can use the in operator to solve this problem.
Method 2:in operator
This method can determine whether a property exists in the own and inherited properties of the specified object, and returns true if so. The in operator can also detect attributes on the prototype chain.
'name' in obj / / true own attribute exists' occupation' in obj / / true non-enumerable attribute there is a property with 'nationality' in obj / / true inheritance attribute' birthday' in obj / / true value undefined
The syntax of the in operator is also very simple, the scope and effect of the point (. ) or square brackets ([]) is the same, except that an attribute with a value of undefined can also be judged normally.
The limitation of these two methods is that they cannot accurately distinguish whether they are their own attributes or those on the prototype chain. Object.hasOwnProperty () is required to detect the existence of one's own attribute.
Method 3:Object.hasOwnProperty ()
Object.hasOwnProperty () is used to determine whether the specified object itself contains a property (non-inheritance) and returns a Boolean value.
Obj.hasOwnProperty ('name') / / true own property obj.hasOwnProperty (' occupation') / / true cannot enumerate properties inherited on the property obj.hasOwnProperty ('birthday') / / trueobj.hasOwnProperty (' nationality') / / false prototype chain
This method filters out those inherited properties and returns true when the detected property is its own.
The above is about the content of this article on "how to judge whether an object has a certain attribute or not". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.