Skip to content

Instantly share code, notes, and snippets.

@zhongyangxun
Created January 21, 2021 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhongyangxun/aef28fe97f7f1c0314982ca19e7eb352 to your computer and use it in GitHub Desktop.
Save zhongyangxun/aef28fe97f7f1c0314982ca19e7eb352 to your computer and use it in GitHub Desktop.
Simulate the `instanceof` keyword.
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