blob: 41bd582c4e3d4c93105b054dba011e36eebfad41 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
---
title: Object.prototype.hasOwnProperty()
slug: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
tags:
- JavaScript
translation_of: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
---
<div>{{JSRef}}</div>
<p><code><strong>hasOwnProperty()</strong></code> 回傳物件是否有該屬性的布林值。</p>
<h2 id="表達式">表達式</h2>
<pre class="syntaxbox"><code><var>obj</var>.hasOwnProperty(<var>prop</var>)</code></pre>
<h3 id="參數">參數</h3>
<dl>
<dt><code>prop</code></dt>
<dd>屬性名稱。</dd>
</dl>
<h2 id="說明">說明</h2>
<p>每個為 {{jsxref("Object")}} 後代的物件都繼承 <code>hasOwnProperty</code> 方法。這個方法可以被使用來決定物件是否擁有特定的直接屬性;跟 {{jsxref("Operators/in", "in")}} 不一樣,這個方法並未檢查物件的原型鏈。</p>
<h2 id="範例">範例</h2>
<h3 id="使用_hasOwnProperty_測試屬性是否存在">使用 <code>hasOwnProperty</code> 測試屬性是否存在</h3>
<p>這個範例顯示 <code>o </code>物件是否擁有名為 <code>prop </code>的屬性:</p>
<pre class="brush: js">o = new Object();
o.prop = 'exists';
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty('prop'); // 回傳 true
changeO();
o.hasOwnProperty('prop'); // 回傳 false
</pre>
<h3 id="直接與繼承的屬性">直接與繼承的屬性</h3>
<p>這個範例區分直接屬性和從原型鍊繼承的屬性:</p>
<pre class="brush: js">o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // 回傳 true
o.hasOwnProperty('toString'); // 回傳 false
o.hasOwnProperty('hasOwnProperty'); // 回傳 false
</pre>
<h3 id="遍歷物件的屬性">遍歷物件的屬性</h3>
<p>這個範例顯示如何不執行繼承的屬性去遍歷物件的屬性。注意 {{jsxref("Statements/for...in", "for...in")}} 已經遍歷了可以被列舉的項目,所以不該基於缺乏不可列舉的屬性(如下)而假設 <code>hasOwnProperty</code> 被嚴格地限制在列舉的項目(如同 {{jsxref("Object.getOwnPropertyNames()")}})。</p>
<pre class="brush: js">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
}
}
</pre>
<h3 id="將_hasOwnProperty_作為屬性"><code>將 hasOwnProperty</code> 作為屬性</h3>
<p>JavaScript 並未保護 <code>hasOwnProperty</code>;因此,如果一個物件擁有一樣的屬性名稱,為了獲得正確的結果需要使用 <em>external</em> <code>hasOwnProperty</code>:</p>
<pre class="brush: js">var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
};
foo.hasOwnProperty('bar'); // 總是回傳 false
// 使用其他物件的 hasOwnProperty 和 call it with 'this' set to foo
({}).hasOwnProperty.call(foo, 'bar'); // true
// 從物件的原型使用 hasOwnProperty 也是可行的
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
</pre>
<p>註:在最後一個例子中並未創建任何新的物件。</p>
<h2 id="規範">規範</h2>
{{Specifications}}
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
{{Compat}}
<h2 id="參見">參見</h2>
<ul>
<li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
<li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
<li>{{jsxref("Statements/for...in", "for...in")}}</li>
<li>{{jsxref("Operators/in", "in")}}</li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Inheritance_Revisited">JavaScript Guide: Inheritance revisited</a></li>
</ul>
|