Created
April 5, 2019 16:34
-
-
Save ositowang/6db12934d72bd5538157b5b9a9c5ae90 to your computer and use it in GitHub Desktop.
inheritance based on prototype chain
This file contains hidden or 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
/** | |
* 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