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')); // true
hasOwnProperty() 方法詳解
hasOwnProperty(propertyName)方法 是用來(lái)檢測(cè)屬性是否為對(duì)象的自有屬性,如果是,返回true,否者false; 參數(shù)propertyName指要檢測(cè)的屬性名;
用法:object.hasOwnProperty(propertyName) // true/false
hasOwnProperty() 方法是 Object 的原型方法(也稱(chēng)實(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)文章
使用js在layui中實(shí)現(xiàn)上傳圖片壓縮
這篇文章主要介紹了使用js在layui中實(shí)現(xiàn)上傳圖片壓縮,layui 是一款采用自身模塊規(guī)范編寫(xiě)的前端 UI 框架,js上傳圖片壓縮百度有很多方法,,需要的朋友可以參考下2019-06-06js實(shí)現(xiàn)無(wú)縫滾動(dòng)雙圖切換效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)無(wú)縫滾動(dòng)雙圖切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07js判斷價(jià)格,必須為數(shù)字且不能為負(fù)數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇js判斷價(jià)格,必須為數(shù)字且不能為負(fù)數(shù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10H5如何實(shí)現(xiàn)喚起APP及調(diào)試bug解決
這篇文章主要為大家介紹了H5如何實(shí)現(xiàn)喚起APP及調(diào)試bug解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05js實(shí)現(xiàn)同一頁(yè)面多個(gè)運(yùn)動(dòng)效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)同一頁(yè)面多個(gè)運(yùn)動(dòng)效果的方法,涉及javascript操作頁(yè)面元素運(yùn)動(dòng)效果的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04D3.js實(shí)現(xiàn)力向?qū)D的繪制教程詳解
力向?qū)D是繪圖的一種算法,實(shí)現(xiàn)了用以模擬粒子物理運(yùn)動(dòng)的?velocity?Verlet?數(shù)值積分器。本文將利用D3.js實(shí)現(xiàn)力向?qū)D的繪制,需要的可以參考一下2022-11-11JavaScript函數(shù)中關(guān)于valueOf和toString的理解
本文給大家介紹JavaScript函數(shù)中關(guān)于valueOf和toString的理解,簡(jiǎn)單的說(shuō)就是需要轉(zhuǎn)換為字符串時(shí),會(huì)調(diào)用toString,需要轉(zhuǎn)換為數(shù)字時(shí)需要調(diào)用valueOf。對(duì)js valueof tostring知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-06-06JavaScript實(shí)現(xiàn)生成動(dòng)態(tài)表格和動(dòng)態(tài)效果的方法詳解
這篇文章主要介紹了如何通過(guò)JavaScript語(yǔ)言實(shí)現(xiàn)動(dòng)圖表格的生成以及動(dòng)態(tài)效果的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-02-02