Assignment to property of primitive value¶
Object.defineProperty will result in a runtime error.
There is one exception: assigning to a property for which a setter has been defined on the corresponding prototype object (such as
String.prototype) will invoke the setter function.
Assignments that rely on setters on prototype objects may work as intended, but this behavior is subtle and hard to understand, and therefore should be avoided.
The following code snippet tries to pad the string
s to a length divisible by eight by mutating its characters:
for (var i=s.length; i%8; ++i) s[i] = ' ';
for (var i=s.length; i%8; ++i) s += ' ';
Ecma International, ECMAScript 2016 Language Specification, Section 12.15: Assignment Operators.
Ecma International, ECMAScript 2016 Language Specification, Section 184.108.40.206: Object.defineProperty.
Common Weakness Enumeration: CWE-704.