Skip to content

Instantly share code, notes, and snippets.

@GianlucaGuarini
Last active December 18, 2015 15:59
Show Gist options
  • Save GianlucaGuarini/5808435 to your computer and use it in GitHub Desktop.
Save GianlucaGuarini/5808435 to your computer and use it in GitHub Desktop.
Easy example to show how to extend an existing Backbone class inheriting its properties to the new children classes
/*
*
* Parent class containing some methods and properties that must be inherited
*
*/
var ParentClass = Backbone.Model.extend({
initialize: function() {
this.set('name','Dear');
},
say_hello: function ( ) {
// property set only on the child
console.log(this.get('greeting'));
}
});
/*
*
* Child class extending the ParentClass
*
*/
var ChildClass = ParentClass.extend({
initialize: function () {
this.constructor.__super__.initialize.apply(this);
this.set('greeting','Hello!');
// inherited method
this.say_hello();
// inherited property
console.log(this.get('name'));
// property set later once the class gets instanced
console.log(this.get('surname'));
}
});
new ChildClass({
surname:'Friends'
});
@GianlucaGuarini
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment