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

54 lines
1.3 KiB
Plaintext

== Why is this an issue?
The ``++for...in++`` statement allows you to loop through the names of all of the properties of an object. The list of properties includes all those properties that were inherited through the prototype chain. This has the side effect of serving up functions when the interest is in data properties. Programs that don't take this into account can fail.
Therefore, the body of every ``++for...in++`` statement should be wrapped in an ``++if++`` statement that filters which properties are acted upon. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype.
=== Noncompliant code example
[source,javascript]
----
for (name in object) {
doSomething(name); // Noncompliant
}
----
=== Compliant solution
[source,javascript]
----
for (name in object) {
if (object.hasOwnProperty(name)) {
doSomething(name);
}
}
----
=== Exceptions
Loops used to clone objects are ignored.
[source,javascript]
----
for (prop in obj) {
a[prop] = obj[prop]; // Compliant by exception
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Restrict what this loop acts on by testing each property.
endif::env-github,rspecator-view[]