Backbonejs Object Inheritance

Demo of Object Inheritance in Backbone

by Budhram Gurung

HTML

<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone.js"></script>

JavaScript

// http://forrst.com/posts/Backbone_js_super_function-4co
Backbone.Model.prototype._super = function(method){
  return this.constructor.__super__[method].apply(this, _.rest(arguments));
}

var animal = Backbone.Model.extend({
  sound: function() { return '(incoherent grunting)'; },
  talk: function() { alert(this.sound()); }
});

var yeti = animal.extend({});
(new yeti).talk();

var cat = animal.extend({
  sound: function() {
    return 'meow';
  }
});

var dog = animal.extend({
  sound: function() {
    return 'woof';
  }
});

(new cat).talk();
(new dog).talk();

// still woofs!
(new dog)._super('talk');