欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中hasOwnProperty方法用法簡(jiǎn)介

 更新時(shí)間:2024年01月04日 15:01:23   作者:前端不加班  
hasOwnProperty(propertyName)方法 是用來(lái)檢測(cè)屬性是否為對(duì)象的自有屬性,如果是,返回true,否者false; 參數(shù)propertyName指要檢測(cè)的屬性名,這篇文章給大家介紹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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論