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

Comments

Projects
None yet
6 participants
@taybin

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

This comment has been minimized.

Show comment
Hide comment
@tbranyen

tbranyen Nov 4, 2011

Collaborator

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.

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

This comment has been minimized.

Show comment
Hide comment
@taybin

taybin Nov 4, 2011

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

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

This comment has been minimized.

Show comment
Hide comment
@wookiehangover

wookiehangover Nov 4, 2011

Collaborator

+1 for making this more explicit in the documentation

Collaborator

wookiehangover commented Nov 4, 2011

+1 for making this more explicit in the documentation

@jashkenas jashkenas closed this in a881ba6 Jan 13, 2012

@fengdi

This comment has been minimized.

Show comment
Hide comment
@fengdi

fengdi May 7, 2014

They are different.

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

same as

initialize:function(){
     //todo
}

fengdi commented May 7, 2014

They are different.

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

same as

initialize:function(){
     //todo
}
@braddunbar

This comment has been minimized.

Show comment
Hide comment
@braddunbar

braddunbar May 7, 2014

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);
  // ...
}
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);
  // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment