When to use default values ​​against initialize constructor on model

So, I'm trying to learn how to use Backbone, and I am constantly switching between using the default object and the initialization method. If I use this method, it sets the attributes with the parameter "this.set ()", etc. Otherwise, these attributes are set in the object by default.

I looked at google and I cannot find the recommended way or the “general” way to use default values ​​or when to use initialization. I can make my code work in both directions, and both give an object with the desired attributes, but it bothers me because I'm not sure if I am using it incorrectly.

+6
source share
1 answer

You would use the default object for all “static” data, since you can only define it once for a model class. You will need the initialize method if you need to add dynamic properties to the instance. For instance:

initialize: function() { this.set({displayName: this.get('firstname') + this.get('lastname')}); } 
+10
source

Source: https://habr.com/ru/post/903398/


All Articles