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

js中hasOwnProperty()方法詳解

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

hasOwnProperty(propertyName)方法 是用來檢測(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ì)象本身來說,這些原型上的屬性又是原型對(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("歡迎來到" + this.name);
? ? };
}

var obj = {
? ? engine: "PHP"
? ? ,sayHi: function(){
? ? ? ? document.writeln("歡迎訪問" + 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)論

桂阳县| 招远市| 甘肃省| 泸定县| 仪征市| 杭锦旗| 孟津县| 濮阳市| 沂水县| 彰武县| 巴林右旗| 莱阳市| 亳州市| 陵川县| 涿鹿县| 云浮市| 昆山市| 丹阳市| 车致| 汤阴县| 翁牛特旗| 浪卡子县| 神木县| 怀集县| 集安市| 桓台县| 上虞市| 库车县| 霍邱县| 兴化市| 什邡市| 石阡县| 昌平区| 平陆县| 东源县| 安宁市| 玛纳斯县| 南投县| 峨边| 凤山县| 泗水县|