Created
September 14, 2017 09:31
-
-
Save colevandersWands/c3700cc8bd4fba6655c610e24d912e87 to your computer and use it in GitHub Desktop.
prototype inheritance, constructor design pattern
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 constructor(name) { | |
new_obj = {}; | |
new_obj.__proto__ = constructor.prototype; | |
new_obj.name = name; | |
return new_obj; | |
} | |
constructor.prototype = {}; | |
constructor.prototype.age = 9; | |
var obj = constructor('tim'); | |
console.log(obj.name) // tim | |
console.log(obj.age) // 9 | |
obj.hasOwnProperty('name') // true | |
obj.hasOwnProperty('age') // false | |
obj.__proto__ = null; | |
console.log(obj.name) // tim | |
console.log(obj.age) // undefined | |
obj.hasOwnProperty('name') // undefined | |
obj.hasOwnProperty('age') // undefined | |
obj.hasOwnProperty = 'eheh'; | |
console.log(obj.hasOwnProperty) // eheh | |
function factory(name, age) { | |
return { name, age } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment