简体   繁体   中英

Why do I always need to clone model attributes in Backbone?

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);

    //...
}

Since objects are passed by reference in ,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM