Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created August 30, 2010 21:15
Show Gist options
  • Save jboesch/558066 to your computer and use it in GitHub Desktop.
Save jboesch/558066 to your computer and use it in GitHub Desktop.
// This is just an example of a quick way of setting up a new instance with an automatic call to "init"
//
// Create our attachClass method
Object.prototype.attachClass = function(name, o){
this[name] = function(){
if(typeof(this.__init) == 'function'){
this.__init.apply(this, arguments);
}
}
this[name].prototype = o;
}
// Declare our site object
var MySiteObj = {};
MySiteObj.attachClass('Person', {
__init: function(o){
alert("Hi, I'm " + o.age + " years old");
this.sex = o.sex;
},
getSex: function(){
return this.sex;
}
});
// Try it out!
var p = new MySiteObj.Person({ age: 15, sex: 'yes PLZ LOL ROFL!!!!' });
alert(p.getSex());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment