Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

58 lines
1.4 KiB
Plaintext

== Why is this an issue?
When using the Backbone.js framework with model ``++defaults++`` that contain arrays or objects, ``++defaults++`` should be defined as a function rather than an object. This is because objects and arrays are passed by reference in JavaScript. So a ``++defaults++`` object that contains arrays or objects is going to set the default value of every instance to point to the same shared object or array.
Use a function instead and a fresh copy of the object or array will be peeled off for each instance.
=== Noncompliant code example
[source,javascript]
----
var Person = Backbone.Model.extend({
defaults: { // Noncompliant; every instance of Person will share the same instance of favoriteColors
favoriteColors: ["blue","purple","raspberry"]
}
});
----
=== Compliant solution
[source,javascript]
----
var Person = Backbone.Model.extend({
defaults: function() {
return {
favoriteColors: ["blue","purple","raspberry"]
};
}
});
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Make "defaults" a function.
'''
== Comments And Links
(visible only on this page)
=== on 16 Mar 2015, 07:58:33 Linda Martin wrote:
Reviewed
=== on 1 Nov 2019, 16:29:58 Elena Vilchik wrote:
https://github.com/SonarSource/SonarJS/issues/1698
endif::env-github,rspecator-view[]