Skip to content

Instantly share code, notes, and snippets.

@ositowang
Created April 5, 2019 16:34
Show Gist options
  • Save ositowang/6db12934d72bd5538157b5b9a9c5ae90 to your computer and use it in GitHub Desktop.
Save ositowang/6db12934d72bd5538157b5b9a9c5ae90 to your computer and use it in GitHub Desktop.
inheritance based on prototype chain
/**
* The most common and buggy prototype chain inheritance.
* You won't use this in production, you swear
* Problems:
* 1. The most famous shared referece bug
*/
function father(name, age) {
this.name = name;
this.age = age;
this.family = ['tommy', 'buddy', 'kathy'];
}
father.prototype.sayHello = function() {
console.log('I am Father');
};
function child() {}
let f = new father('daddy', 100);
child.prototype = f;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment