-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Default values are passed in by reference #1145
Comments
"Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances." http://backbonejs.org/#Model-defaults This is expected behavior, you can use a function for defaults and return an object that contains a reference type. defaults: function() {
return {
shared: []
};
} |
Yes, this is intentional. For different behavior |
I was struggling with this until I can across this issue here. Perhaps it would be beneficial to update the documentation to explain this slightly. Currently it reads: "Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances." Perhaps we could change it to: "Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances. Defaults containing objects passed by reference should be defined using a function if you do not wish to share objects between all instances." |
@Benjen That sounds reasonable to me. Care to work up a pull request to that effect? |
…n, to explain when to use a function to define model default values. See Issue jashkenas#1145
Issue #1145: Changed documentation to elaborate when to use a function to define model defaults.
Having an empty array as a default value can definitely trick the user:
http://jsfiddle.net/bika/Kn5sP/2/
Is this intended?
The text was updated successfully, but these errors were encountered: