Skip to content

Commit 3b60ec6

Browse files
committed
add some example.
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent afd9f18 commit 3b60ec6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

doc/zhtw/object/hasownproperty.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## `hasOwnProperty`
1+
## `hasOwnProperty`
22

33
為了判斷一個物件是否包含 *自定義* 屬性而 *不是* [原形](#object.prototype)上的屬性,我們需要使用繼承 `Object.prototype``hasOwnProperty` 方法。
44

@@ -8,9 +8,9 @@
88
`hasOwnProperty` 是 JavaScript 中唯一一個處理屬性但是 **** 找原型鏈的函式。
99

1010
// 修改 Object.prototype
11-
Object.prototype.bar = 1;
11+
Object.prototype.bar = 1;
1212
var foo = {goo: undefined};
13-
13+
1414
foo.bar; // 1
1515
'bar' in foo; // true
1616

@@ -35,6 +35,8 @@ JavaScript **不會** 保護 `hasOwnProperty`被占用,因此如果碰到存
3535
// 使用其他對象的 hasOwnProperty,並將其上下設置為 foo
3636
({}).hasOwnProperty.call(foo, 'bar'); // true
3737

38+
// 也可以透過原生 Object prototype 的 hasOwnProperty 函數來達成目的
39+
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
3840

3941
### 結論
4042

0 commit comments

Comments
 (0)