-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangjin
committed
Nov 13, 2019
1 parent
53bbd9f
commit 193c1ae
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |