最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS中hasOwnProperty方法用法簡介

 更新時間:2024年01月04日 15:01:23   作者:前端不加班  
hasOwnProperty(propertyName)方法 是用來檢測屬性是否為對象的自有屬性,如果是,返回true,否者false; 參數(shù)propertyName指要檢測的屬性名,這篇文章給大家介紹JS中hasOwnProperty方法用法簡介,感興趣的朋友一起看看吧

JS中hasOwnProperty方法用法簡介

hasOwnProperty表示是否有自己的屬性。這個方法會查找一個對象是否有某個屬性,但是不會去查找它的原型鏈。

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對象沒有d屬性
var str = new String();
// split方法是String這個對象的方法,str對象本身是沒有這個split這個屬性的
console.log(str.hasOwnProperty('split'));  // false
console.log(String.prototype.hasOwnProperty('split'));  // true

hasOwnProperty() 方法詳解

hasOwnProperty(propertyName)方法 是用來檢測屬性是否為對象的自有屬性,如果是,返回true,否者false; 參數(shù)propertyName指要檢測的屬性名;

用法:object.hasOwnProperty(propertyName) // true/false

hasOwnProperty() 方法是 Object 的原型方法(也稱實(shí)例方法),它定義在 Object.prototype 對象之上,所有 Object 的實(shí)例對象都會繼承 hasOwnProperty() 方法。

hasOwnProperty() 只會檢查對象的自有屬性,對象原形上的屬性其不會檢測;但是對于原型對象本身來說,這些原型上的屬性又是原型對象的自有屬性,所以原形對象也可以使用hasOwnProperty()檢測自己的自有屬性;

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方法用法簡介的文章就介紹到這了,更多相關(guān)js hasOwnProperty用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

铜山县| 天柱县| 如东县| 白朗县| 东丽区| 斗六市| 分宜县| 宝清县| 兴文县| 托克托县| 遂川县| 四川省| 双峰县| 阳新县| 即墨市| 临澧县| 孝感市| 张掖市| 旬邑县| 合水县| 农安县| 阿城市| 金山区| 五家渠市| 南岸区| 苍梧县| 丰顺县| 团风县| 乡宁县| 五指山市| 余姚市| 彭州市| 汾阳市| 云安县| 建德市| 曲松县| 沁源县| 府谷县| 梁河县| 奉新县| 石渠县|