Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fundamental differences between utilizing CoffeeScript `extends` vs. Backbone.js `extend`

What are the fundamental differences between utilizing CoffeeScript extends vs. Backbone.js extend?

For example, how is

class User extends Backbone.Model

different from

User = Backbone.Model.extend()
like image 836
rudolph9 Avatar asked Nov 06 '12 02:11

rudolph9


1 Answers

The two are intended to be equivalent. To quote the Backbone.js changelog:

0.3.0: Backbone classes may now be seamlessly inherited by CoffeeScript classes.

Both CoffeeScript's Child extends Parent and Backbone's Child = Parent.extend() do three important things:

  1. (Most important) They set Child.prototype to new ctor, where ctor is a function whose prototype is Parent.prototype. That establishes prototypal inheritance.
  2. They copy all of Parent's static properties onto Child.
  3. They set Child.__super__ = Parent. This is mainly to support CoffeeScript's Ruby-like super keyword in Child's methods.
like image 174
Trevor Burnham Avatar answered Nov 08 '22 06:11

Trevor Burnham