Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize versus constructor #720

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

initialize versus constructor #720

taybin opened this issue Nov 4, 2011 · 5 comments

Comments

@taybin
Copy link

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.

@tbranyen
Copy link
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
Copy link
Author

taybin commented Nov 4, 2011

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

@wookiehangover
Copy link
Collaborator

+1 for making this more explicit in the documentation

@fengdi
Copy link

fengdi commented May 7, 2014

They are different.

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

same as

initialize:function(){
     //todo
}

@braddunbar
Copy link
Collaborator

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);
  // ...
}

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

No branches or pull requests

6 participants