Skip to content

Commit

Permalink
feat: 添加实现 instanceof 笔记
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjin committed Nov 13, 2019
1 parent 53bbd9f commit 193c1ae
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/javaScript/实现instanceof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 实现 instanceof

`instanceof` 用于判断构造函数的 `prototype` 属性是否出现在一个对象的原型链中。

```js
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car); // true
console.log(auto instanceof Object); // true
```
## instanceof

```js
/**
* instanceof
*
* @param {Object} obj 需要判断的对象.
* @param {Function} ctor 构造函数.
* @returns {Boolean} 返回判断结果.
*/
function instanceof(obj, ctor) {
var proto = obj.__proto__;
var prototype = ctor.prototype;
while (true) {
if (proto == null) return false;
if (proto == prototype) return true;
proto = proto.__proto__;
}
}
```

## 参考

- [instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof)
- [instanceof原理](https://juejin.im/post/5b7f64be51882542c83476f0)

0 comments on commit 193c1ae

Please sign in to comment.