国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > JS > 正文

JS Testing Properties 判斷屬性是否在對(duì)象里的方法

2024-05-06 16:40:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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)心繼承來(lái)的屬性。

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è)參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 渭南市| 军事| 伽师县| 宝应县| 高碑店市| 精河县| 洛浦县| 大丰市| 定南县| 宝兴县| 儋州市| 大兴区| 西充县| 西乌珠穆沁旗| 德庆县| 龙口市| 瑞安市| 大关县| 滦平县| 屯留县| 长春市| 镇沅| 桓台县| 赣榆县| 慈溪市| 扎鲁特旗| 阿尔山市| 涪陵区| 武宁县| 昌江| 舟曲县| 枝江市| 定南县| 平乐县| 南宫市| 扶余县| 扎囊县| 内丘县| 兴义市| 富裕县| 鲁山县|