Reputation: 16501
I'm overriding the Backbone Model toJSON to reformat some data, I see _.clone() a lot and I've seen that I need to clone this.attributes. I'm not completely sure why I need to clone, can anyone explain?
JS
toJSON: function()
var attributes = _.clone(this.attributes);
//...
}
Upvotes: 0
Views: 79
Reputation: 43156
Since objects are passed by reference in javascript,
If you do this:
var attributes = this.attributes;
Whatever changes you make to attributes
will reflect in the actual model as well.
Most of the time this is not the desired behavior, hence the use of _.clone
or similar utility methods
Upvotes: 1