Skip to content

Instantly share code, notes, and snippets.

@colevandersWands
Created September 14, 2017 09:31
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 colevandersWands/c3700cc8bd4fba6655c610e24d912e87 to your computer and use it in GitHub Desktop.
Save colevandersWands/c3700cc8bd4fba6655c610e24d912e87 to your computer and use it in GitHub Desktop.
prototype inheritance, constructor design pattern
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