Skip to content

Instantly share code, notes, and snippets.

@k33g
Created April 2, 2012 20:29
Show Gist options
  • Save k33g/2287018 to your computer and use it in GitHub Desktop.
Save k33g/2287018 to your computer and use it in GitHub Desktop.
Re Use Object Model of BackBone
// Just do this : (and include backbone.js)
var Kind = function() {
this.initialize && this.initialize.apply(this, arguments);
};
Kind.extend = Backbone.Model.extend
//Simpler
var Thing = function() {};
Thing.extend = Backbone.Model.extend
// Use it :
var Human = Kind.extend({
toString : function() { console.log("hello : ", this); },
initialize : function (name) {
console.log("human constructor");
this.name = name
}
});
//Someone inherits of Human
var SomeOne = Human.extend({
initialize : function(name){
//call parent constructor
SomeOne.__super__.initialize.call(this, name);
}
});
var Bob = new Human("Bob");
Bob.toString();
var Sam = new SomeOne("Sam");
Sam.toString();
// Use it :
var Human = Thing.extend({
toString : function() { console.log("hello : ", this); },
constructor : function Human (name) {
console.log("human constructor");
this.name = name
}
});
//Someone inherits of Human
var SomeOne = Human.extend({
constructor : function SomeOne (name){
//call parent constructor
SomeOne.__super__.constructor.call(this, name);
}
});
var Bob = new Human("Bob");
Bob.toString();
var Sam = new SomeOne("Sam");
Sam.toString();
//Static members
var Human = Kind.extend({
toString : function() { console.log("hello : ", this); },
initialize : function (name) {
console.log("human constructor");
this.name = name
}
},{ //Static
counter : 0,
getCounter : function() { return this.counter; }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment