Skip to content

Instantly share code, notes, and snippets.

@richcollins
Created August 21, 2008 03:16
Show Gist options
  • Save richcollins/6494 to your computer and use it in GitHub Desktop.
Save richcollins/6494 to your computer and use it in GitHub Desktop.
Jovian Javascript
Object.prototype.setSlots = function(slots)
{
for(var slotName in slots)
{
if(slots.hasOwnProperty(slotName))
{
this[slotName] = slots[slotName];
}
}
return this;
}
Object.prototype.setSlots({
clone: function()
{
var constructor = function(){};
constructor.prototype = this;
return new constructor;
},
newSlots: function()
{
var args = arguments;
var i = args.length;
while(i-- > 0) {
this.newSlot(args[i]);
}
return this;
},
newSlot: function(slotName, initialValue)
{
if(arguments.length > 2)
{
throw "newSlot expected 2 arguments but there were " + arguments.length;
}
this[slotName] = function(){ return initialValue; };
this['set' + slotName.asCapitalized()] = function(newValue){ initialValue = newValue; return this; };
return this;
}
});
var Dog = Object.prototype.clone()
.newSlot("voice", "Woof!")
.setSlots({
speak: function()
{
console.log(this.voice());
}
});
Dog.speak();
var sugar = Dog.clone().setVoice("Ruff!");
sugar.speak();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment