Skip to content

Instantly share code, notes, and snippets.

@reconbot
Created February 15, 2011 23:21
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 reconbot/828500 to your computer and use it in GitHub Desktop.
Save reconbot/828500 to your computer and use it in GitHub Desktop.
How would I create a constructor factory?
// 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();
// =(
@reconbot
Copy link
Author

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