Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Created November 10, 2018 02:17
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 Phrogz/1051d7bb8e70d068957a5c01c0eaed75 to your computer and use it in GitHub Desktop.
Save Phrogz/1051d7bb8e70d068957a5c01c0eaed75 to your computer and use it in GitHub Desktop.
Intercepting JavaScript prototypal inheritance
let p = console.log;
let Interceptor = { hi:_=>"I am "+this.name }
function Person(name) {
this.name = name;
}
Person.prototype.hi = function() {
return "Hello, I'm "+this.name
}
let bob = new Person('Bob');
p( bob.hi() );
// Hello, I'm bob
Object.setPrototypeOf(bob, Interceptor);
p( bob.hi() );
// I am undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment