JS中hasOwnProperty方法用法簡(jiǎn)介
JS中hasOwnProperty方法用法簡(jiǎn)介
hasOwnProperty表示是否有自己的屬性。這個(gè)方法會(huì)查找一個(gè)對(duì)象是否有某個(gè)屬性,但是不會(huì)去查找它的原型鏈。
var obj = {
a: 1,
fn: function(){
},
c:{
d: 5
}
};
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('fn')); // true
console.log(obj.hasOwnProperty('c')); // true
console.log(obj.c.hasOwnProperty('d')); // true
console.log(obj.hasOwnProperty('d')); // false, obj對(duì)象沒(méi)有d屬性
var str = new String();
// split方法是String這個(gè)對(duì)象的方法,str對(duì)象本身是沒(méi)有這個(gè)split這個(gè)屬性的
console.log(str.hasOwnProperty('split')); // false
console.log(String.prototype.hasOwnProperty('split')); // truehasOwnProperty() 方法詳解
hasOwnProperty(propertyName)方法 是用來(lái)檢測(cè)屬性是否為對(duì)象的自有屬性,如果是,返回true,否者false; 參數(shù)propertyName指要檢測(cè)的屬性名;
用法:object.hasOwnProperty(propertyName) // true/false
hasOwnProperty() 方法是 Object 的原型方法(也稱實(shí)例方法),它定義在 Object.prototype 對(duì)象之上,所有 Object 的實(shí)例對(duì)象都會(huì)繼承 hasOwnProperty() 方法。

hasOwnProperty() 只會(huì)檢查對(duì)象的自有屬性,對(duì)象原形上的屬性其不會(huì)檢測(cè);但是對(duì)于原型對(duì)象本身來(lái)說(shuō),這些原型上的屬性又是原型對(duì)象的自有屬性,所以原形對(duì)象也可以使用hasOwnProperty()檢測(cè)自己的自有屬性;
let obj = {
name:'張睿',
age:18,
eat:{
eatname:'面條',
water:{
watername:'農(nóng)夫山泉'
}
}
}
console.log(obj.hasOwnProperty('name')) //true
console.log(obj.hasOwnProperty('age')) //true
console.log(obj.hasOwnProperty('eat')) //true
console.log(obj.hasOwnProperty('eatname')) //false
console.log(obj.hasOwnProperty('water')) //false
console.log(obj.hasOwnProperty('watername')) //false
console.log(obj.eat.hasOwnProperty('eatname')) //true
console.log(obj.eat.hasOwnProperty('water')) //true
console.log(obj.eat.hasOwnProperty('watername')) //false
console.log(obj.eat.water.hasOwnProperty('watername')) //true到此這篇關(guān)于JS中hasOwnProperty方法用法簡(jiǎn)介的文章就介紹到這了,更多相關(guān)js hasOwnProperty用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- js中hasOwnProperty()方法詳解
- js中hasOwnProperty的屬性及實(shí)例用法詳解
- JavaScript中in和hasOwnProperty區(qū)別詳解
- JS中的hasOwnProperty()和isPrototypeOf()屬性實(shí)例詳解
- javascript中hasOwnProperty() 方法使用指南
- js中的hasOwnProperty和isPrototypeOf方法使用實(shí)例
- JavaScript isPrototypeOf和hasOwnProperty使用區(qū)別
- 使用hasOwnProperty時(shí)報(bào)錯(cuò)的解決方法
相關(guān)文章
關(guān)于js new Date() 出現(xiàn)NaN 的分析
在一個(gè)項(xiàng)目中需要進(jìn)行日期的格式化,后臺(tái)傳到前端是時(shí)間的整數(shù)(Date.getTime),當(dāng)后臺(tái)數(shù)據(jù)返回字符串時(shí),發(fā)現(xiàn)轉(zhuǎn)換日期時(shí)在ie下變成NaN,但是真的是這樣嗎?接下來(lái)我們慢慢分析2012-10-10
JavaScript中關(guān)于Object.create()的用法
這篇文章主要介紹了JavaScript中關(guān)于Object.create()的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
js 輸入框 正則表達(dá)式(菜鳥(niǎo)必看教程)
下面小編就為大家?guī)?lái)一篇js輸入框使用正則表達(dá)式校驗(yàn)輸入內(nèi)容的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
layui關(guān)閉層級(jí)、簡(jiǎn)單監(jiān)聽(tīng)的實(shí)例
今天小編就為大家分享一篇layui關(guān)閉層級(jí)、簡(jiǎn)單監(jiān)聽(tīng)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
js 在定義的時(shí)候立即執(zhí)行的函數(shù)表達(dá)式(function)寫(xiě)法
如果不需要顯示調(diào)用函數(shù), 讓這個(gè)函數(shù)在定義的時(shí)候就執(zhí)行的話, 該如何寫(xiě)才可以呢,接下來(lái)將詳細(xì)介紹實(shí)現(xiàn)步驟,感興趣的朋友可以了解下2013-01-01
js獲取觸發(fā)事件元素在整個(gè)網(wǎng)頁(yè)中的絕對(duì)坐標(biāo)(示例代碼)
這篇文章主要介紹了js獲取觸發(fā)事件元素在整個(gè)網(wǎng)頁(yè)中的絕對(duì)坐標(biāo)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12

