您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“JS中Testing Properties如何判斷屬性是否在對象里”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“JS中Testing Properties如何判斷屬性是否在對象里”這篇文章吧。
Testing Properties
To check whether an object has a property with a given name. You can do this with the in operator, with the hasOwnProperty() and propertyIsEnumerable() methods,
在JS中判斷一個對象是否包含某個屬性,可以使用 in,hasOwnProperty() and propertyIsEnumerable()
or simply by querying the property.
或者直接使用查詢屬性。
in--It returns true if the object has an own property or an inherited property
用In,當前對象存在或者有繼承,就返回true。
hasOwnProperty() --To test whether that object has an own property with the given name. It returns false for inherited properties
用hasOwnProperty() ,只關心本對象,不關心繼承來的屬性。
propertyIsEnumerable()--The propertyIsEnumerable() refines the hasOwnProperty() test. It returns true only if the named property is an own property and its enumerable attribute is true.
用propertyIsEnumerable() ,和hasOwnProperty() 這個類似,只是要求 屬性可枚舉。
Instead of using the in operator it is often sufficient to simply query the property and use !== to make sure it is not undefined
o.x !== undefined; // true: o has a property x
替代In的最簡單辦法就是 query + !==Undefined
in can distinguish between properties that do not exist and properties that exist but have been set to undefined.
in 有個好處就是還能區分到底屬性的值是undefined還是本身就不存在。
以上是“JS中Testing Properties如何判斷屬性是否在對象里”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。