initialize versus constructor #720

Closed
taybin opened this Issue Nov 4, 2011 · 5 comments

Comments

Projects
None yet
6 participants

taybin commented Nov 4, 2011

When would I want to use initialize() and when would I want to use constructor()? The documentation make it look like they are synonyms, but the source shows they handled differently.

Collaborator

tbranyen commented Nov 4, 2011

constructor runs before Backbone sets up the structure. initialize is called inside the structure's constructor function. So basically if you need to augment anything before Backbone sets up the structure, use constructor if you need to augment anything after Backbone sets up the structure use initialize.

taybin commented Nov 4, 2011

Thanks, that makes sense. I'd mark this closed, but it looks like you want to update the documentation?

Collaborator

wookiehangover commented Nov 4, 2011

+1 for making this more explicit in the documentation

jashkenas closed this in a881ba6 Jan 13, 2012

fengdi commented May 7, 2014

They are different.

constructor:function(){
    TestModel.__super__.constructor.apply(this, arguments);
    //todo   
 }

same as

initialize:function(){
     //todo
}
Collaborator

braddunbar commented May 7, 2014

Hi @fengdi! You're right but using __super__ is not recommended and instead you should use the parent directly.

constructor: function() {
  Parent.apply(this, arguments);
  // ...
}

ssunkara1 referenced this issue in jupyter-widgets/ipywidgets Oct 13, 2016

Closed

Order of functions called in WidgetModel #829

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