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

js中hasOwnProperty()方法詳解

 更新時(shí)間:2023年04月21日 10:16:56   作者:Miss_Mr_Rui  
本文主要介紹了js中hasOwnProperty()方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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

例子:

function Site(){
? ? this.name = "CodePlayer";
? ? this.url = "http://www.365mini.com/";

? ? this.sayHello = function(){
? ? ? ? document.writeln("歡迎來(lái)到" + this.name);
? ? };
}

var obj = {
? ? engine: "PHP"
? ? ,sayHi: function(){
? ? ? ? document.writeln("歡迎訪問(wèn)" + this.url);
? ? }
};
// 使用對(duì)象obj覆蓋Site本身的prototype屬性
Site.prototype = obj;

var s = ?new Site();
document.writeln( s.hasOwnProperty("name") ); // true
document.writeln( s.hasOwnProperty("sayHello") ); // true
// 以下屬性繼承自原型鏈,因此為false
document.writeln( s.hasOwnProperty("engine") ); // false
document.writeln( s.hasOwnProperty("sayHi") ); // false
document.writeln( s.hasOwnProperty("toString") ); // false

// 想要查看對(duì)象(包括原型鏈)是否具備指定的屬性,可以使用in操作符
document.writeln( "engine" in s ); // true
document.writeln( "sayHi" in s ); // true
document.writeln( "toString" in s ); // true

 到此這篇關(guān)于js中hasOwnProperty()方法詳解的文章就介紹到這了,更多相關(guān)js hasOwnProperty()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

井陉县| 缙云县| 仁寿县| 翁牛特旗| 陵水| 阿克陶县| 大田县| 肥城市| 资溪县| 鹰潭市| 平湖市| 乐安县| 绥滨县| 娱乐| 文化| 林周县| 邵阳市| 汝南县| 阿城市| 青海省| 娱乐| 佛山市| 孝义市| 剑阁县| 衡南县| 红桥区| 平利县| 襄樊市| 正镶白旗| 永昌县| 广州市| 宣武区| 奉节县| 棋牌| 那坡县| 塔城市| 宝山区| 武冈市| 乐都县| 河北省| 宁海县|