日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript 中 obj.hasOwnProperty(prop) 方法

發(fā)布時(shí)間:2023/12/13 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript 中 obj.hasOwnProperty(prop) 方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

語法

obj.hasOwnProperty(prop)

參數(shù)

prop

要檢測(cè)的屬性的?String?字符串形式表示的名稱,或者?Symbol。

返回值

用來判斷某個(gè)對(duì)象是否含有指定的屬性的布爾值?Boolean。

描述

所有繼承了?Object?的對(duì)象都會(huì)繼承到?hasOwnProperty?方法。這個(gè)方法可以用來檢測(cè)一個(gè)對(duì)象是否含有特定的自身屬性;和?in?運(yùn)算符不同,該方法會(huì)忽略掉那些從原型鏈上繼承到的屬性。

備注

即使屬性的值是?null?或?undefined,只要屬性存在,hasOwnProperty?依舊會(huì)返回?true。

o = new Object(); o.propOne = null; o.hasOwnProperty('propOne'); // 返回 true o.propTwo = undefined; o.hasOwnProperty('propTwo'); // 返回 true

示例

使用?hasOwnProperty?方法判斷屬性是否存在

下面的例子檢測(cè)了對(duì)象?o?是否含有自身屬性?prop:

o = new Object(); o.hasOwnProperty('prop'); // 返回 false o.prop = 'exists'; o.hasOwnProperty('prop'); // 返回 true delete o.prop; o.hasOwnProperty('prop'); // 返回 false

自身屬性與繼承屬性

下面的例子演示了?hasOwnProperty?方法對(duì)待自身屬性和繼承屬性的區(qū)別:

o = new Object(); o.prop = 'exists'; o.hasOwnProperty('prop'); // 返回 true o.hasOwnProperty('toString'); // 返回 false o.hasOwnProperty('hasOwnProperty'); // 返回 false

遍歷一個(gè)對(duì)象的所有自身屬性

下面的例子演示了如何在遍歷一個(gè)對(duì)象的所有屬性時(shí)忽略掉繼承屬性,注意這里?for...in? 循環(huán)只會(huì)遍歷可枚舉屬性,所以不應(yīng)該基于這個(gè)循環(huán)中沒有不可枚舉的屬性而得出?hasOwnProperty?是嚴(yán)格限制于可枚舉項(xiàng)目的(如同?Object.getOwnPropertyNames())。

var buz = {fog: 'stack' };for (var name in buz) {if (buz.hasOwnProperty(name)) {console.log('this is fog (' +name + ') for sure. Value: ' + buz[name]);}else {console.log(name); // toString or something else} }

使用?hasOwnProperty?作為屬性名

JavaScript 并沒有保護(hù)?hasOwnProperty?這個(gè)屬性名,因此,當(dāng)某個(gè)對(duì)象可能自有一個(gè)占用該屬性名的屬性時(shí),就需要使用外部的?hasOwnProperty?獲得正確的結(jié)果:

var foo = {hasOwnProperty: function() {return false;},bar: 'Here be dragons' };foo.hasOwnProperty('bar'); // 始終返回 false// 如果擔(dān)心這種情況, // 可以直接使用原型鏈上真正的 hasOwnProperty 方法 ({}).hasOwnProperty.call(foo, 'bar'); // true// 也可以使用 Object 原型上的 hasOwnProperty 屬性 Object.prototype.hasOwnProperty.call(foo, 'bar'); // true

注意,只有在最后一種情況下,才不會(huì)新建任何對(duì)象。

直接上代碼來個(gè)示例:

function ObjWithProto(){this.foo = 'foo_val' } ObjWithProto.prototype.bar = 'bar_val' var dict = new ObjWithProto() dict.foobar = 'foobar_val'dict.hasOwnProperty('foo') // true dict.hasOwnProperty('foobar') // true dict.hasOwnProperty('bar') // false

再來看 for…in , 遍歷一個(gè)對(duì)象的可枚舉屬性

for(let i in dict){console.log(i) } //foo // foobar // bar //原型鏈上的bar也獲取到了

為了遍歷一個(gè)對(duì)象的所有屬性時(shí)忽略掉繼承屬性,使用hasOwnProperty()來過濾該對(duì)象上的繼承屬性。

for(let i in dict){if(dict.hasOwnProperty(i)){console.log(i)} } //foo //foobar

再來看看原型連上的一個(gè)繼承

function ObjWithProto(){this.foo = 'foo_val'}ObjWithProto.prototype.bar = 'bar_val'function Person(){this.name = 'Person_name'}Person.prototype = new ObjWithProto()var _child = new Person()for(let i in _child){console.log(i)}console.log('------ this is a line -------')for(let i in _child){if(_child.hasOwnProperty(i)){console.log(i)}}_child.hasOwnProperty('name') // true_child.hasOwnProperty('foo') // false_child.hasOwnProperty('bar') // false//name//foo//bar//------ this is a line -------//name

用for...in循環(huán)會(huì)獲取到原型鏈上的可枚舉屬性,不過可以使用hasOwnProperty()方法過濾掉。

總結(jié)

以上是生活随笔為你收集整理的JavaScript 中 obj.hasOwnProperty(prop) 方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。