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,當(dāng)前對象存在或者有繼承,就返回true。
hasOwnProperty() --To test whether that object has an own property with the given name. It returns false for inherited properties
用hasOwnProperty() ,只關(guān)心本對象,不關(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() 這個類似,只是要求 屬性可枚舉。
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 有個好處就是還能區(qū)分到底屬性的值是undefined還是本身就不存在。
以上這篇JS Testing Properties 判斷屬性是否在對象里的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Express實(shí)現(xiàn)前端后端通信上傳圖片之存儲數(shù)據(jù)庫(mysql)傻瓜式教程(二)
這篇文章主要介紹了Express實(shí)現(xiàn)前端后端通信上傳圖片之存儲數(shù)據(jù)庫(mysql)傻瓜教程(二)的相關(guān)資料,需要的朋友可以參考下2015-12-12
webpack4 配置 ssr 環(huán)境遇到“document is not defined”
這篇文章主要介紹了webpack4 配置 ssr 環(huán)境遇到“document is not defined”,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
滾動條變色 隱藏滾動條與雙擊網(wǎng)頁自動滾屏顯示代碼
滾動條變色 隱藏滾動條與雙擊網(wǎng)頁自動滾屏顯示代碼2009-12-12
bootstrap實(shí)現(xiàn)動態(tài)進(jìn)度條效果
本篇文章主要介紹了bootstrap實(shí)現(xiàn)動態(tài)進(jìn)度條效果,進(jìn)度條可以加強(qiáng)應(yīng)用的用戶體驗(yàn)效果,看到數(shù)字,具有一定的參考價值,有興趣的可以了解一下。2017-03-03
blob轉(zhuǎn)換成string格式同步調(diào)用問題解決分析
這篇文章主要為大家介紹了blob轉(zhuǎn)換成string格式同步調(diào)用問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

