JS Testing Properties 判斷屬性是否在對(duì)象里的方法
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中判斷一個(gè)對(duì)象是否包含某個(gè)屬性,可以使用 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,當(dāng)前對(duì)象存在或者有繼承,就返回true。
hasOwnProperty() --To test whether that object has an own property with the given name. It returns false for inherited properties
用hasOwnProperty() ,只關(guān)心本對(duì)象,不關(guān)心繼承來的屬性。
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() 這個(gè)類似,只是要求 屬性可枚舉。
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的最簡(jiǎn)單辦法就是 query + !==Undefined
in can distinguish between properties that do not exist and properties that exist but have been set to undefined.
in 有個(gè)好處就是還能區(qū)分到底屬性的值是undefined還是本身就不存在。
以上這篇JS Testing Properties 判斷屬性是否在對(duì)象里的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
原生js實(shí)現(xiàn)簡(jiǎn)單的模態(tài)框示例
下面小編就為大家?guī)硪黄鷍s實(shí)現(xiàn)簡(jiǎn)單的模態(tài)框示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09js調(diào)用打印機(jī)打印網(wǎng)頁(yè)字體總是縮小一號(hào)的解決方法
直接調(diào)用window.print(),但是打印出來后,字體總是縮小一號(hào),后來直接target="_blank",就可以正常打印了,下面是實(shí)現(xiàn)代碼2014-01-01JavaScript switch case 的用法實(shí)例[范圍]
JavaScript switch case 的用法實(shí)例,大家可以參考下。2009-09-09使用js實(shí)現(xiàn)的簡(jiǎn)單拖拽效果
本文給大家分享的是使用純JS實(shí)現(xiàn)的簡(jiǎn)單的拖拽效果的插件,算是對(duì)自己javascript學(xué)習(xí)的一個(gè)小的檢驗(yàn),如果小伙伴們需要復(fù)雜的拖拽效果,還是考慮jQuery的draggable吧,更成熟一些。2015-03-03javascript實(shí)現(xiàn)仿銀行密碼輸入框效果的代碼
這篇文章通過實(shí)例代碼給大家介紹了javascript實(shí)現(xiàn)仿銀行密碼輸入框效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2007-12-12JavaScript實(shí)現(xiàn)獲得所有兄弟節(jié)點(diǎn)的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)獲得所有兄弟節(jié)點(diǎn)的方法,實(shí)例分析了javascript節(jié)點(diǎn)遍歷的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07