Skip to content

Commit 521bf2d

Browse files
committed
docs(oop): edit prototype/instanceof
1 parent 36fdb74 commit 521bf2d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

docs/oop/prototype.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ d instanceof Object // true
282282

283283
上面代码中,`d`同时是`Date``Object`的实例,因此对这两个构造函数都返回`true`
284284

285+
由于任意对象(除了`null`)都是`Object`的实例,所以`instanceof`运算符可以判断一直是否为非`null`的对象。
286+
287+
```javascript
288+
var obj = { foo: 123 };
289+
obj instanceof Object // true
290+
291+
null instanceof Object // false
292+
```
293+
294+
上面代码中,除了`null`,其他对象的`instanceOf Object`的运算结果都是`true`
295+
285296
`instanceof`的原理是检查右边构造函数的`prototype`属性,是否在左边对象的原型链上。有一种特殊情况,就是左边对象的原型链上,只有`null`对象。这时,`instanceof`判断会失真。
286297

287298
```javascript

0 commit comments

Comments
 (0)