Created
February 15, 2011 23:21
-
-
Save reconbot/828500 to your computer and use it in GitHub Desktop.
How would I create a constructor factory?
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
// http://oranlooney.com/functional-javascript/ | |
function Clone() { } | |
function clone(obj) { | |
Clone.prototype = obj; | |
return new Clone(); | |
} | |
Food = function(type, slices){ | |
this.type = type; | |
this.slices= slices; | |
this.eat= function(){ | |
this.slices --; | |
console.log('I ATE 1 Slice I have ' + this.slices + " left!"); | |
} | |
this.status = function(){ | |
console.log(this, 'I am a ' + this.type + ' and I have ' + this.slices + " slices."); | |
} | |
this.toppings = {}; | |
} | |
Pizza = new Food('pizza', 8); | |
Pizza.status(); | |
my_pizza = clone(Pizza); | |
my_pizza.status(); | |
my_pizza.eat(); | |
my_pizza.status(); | |
Pizza.status(); | |
// But what about the DEEP toppings object? | |
my_pizza.toppings.peperoni = 'OH GOD YES'; | |
my_pizza.status(); | |
Pizza.status(); | |
// =( |
I did what I think is a shallow clone - a prototype inheritance http://oranlooney.com/functional-javascript/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about this? :)