Created
January 21, 2021 15:24
-
-
Save zhongyangxun/aef28fe97f7f1c0314982ca19e7eb352 to your computer and use it in GitHub Desktop.
Simulate the `instanceof` keyword.
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
function myInstanceOf(instance, constructor) { | |
if (typeof constructor !== 'function') { | |
throw Error(`The parameter 'constructor' is not a function!`) | |
} | |
let proto = instance.__proto__; | |
const prototype = constructor.prototype; | |
while (proto !== null) { | |
if (prototype === proto) { | |
return true; | |
} | |
proto = proto.__proto__; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment