Overwritten property¶
ID: js/overwritten-propertyKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - correctness - external/cwe/cwe-563Query suites: - javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
In ECMAScript 2015 and above, as well as ECMAScript 5 non-strict mode, an object literal may define the same property multiple times, with later definitions overwriting earlier ones. In particular, if the last definition assigns a different value from earlier definitions, the earlier value is lost, which is most likely unintentional and should be avoided.
Recommendation¶
Rename the properties to have different names.
Example¶
In the following example, the object literal assigned topoint has two definitions of propertyx. The object’s use in functiondist suggests that the second definition should define a propertyy instead, and should be renamed.
functiondist(p){returnMath.sqrt(p.x*p.x+p.y*p.y);}varpoint={x:3,x:4};console.log(dist(point));
References¶
Ecma International,ECMAScript Language Definition, 5.1 Edition, Annex C. ECMA, 2011.
Ecma International,ECMAScript Language Definition, 6th Edition, Annex E. ECMA, 2015.
Common Weakness Enumeration:CWE-563.