The JavaScript strict mode exception "can't assign to property" occurs when attempting to create a property on primitive value such as a symbol , string , number 或 boolean . Primitive values cannot hold any property .
TypeError: can't assign to property "x" on {y}: not an object (Firefox)
TypeError: Cannot create property 'x' on {y} (Chrome)
在
Strict_mode
,
TypeError
is raised when attempting to create a property on
primitive
value such as a
symbol
,
string
,
number
或
boolean
.
Primitive
values cannot hold any
property
.
The problem might be that an unexpected value is flowing at an unexpected place, or that an object variant of a
String
或
Number
is expected.
'use strict';
var foo = "my string";
// The following line does nothing if not in strict mode.
foo.bar = {}; //
Either fix the code to prevent the
primitive
from being used in such places, or fix the issue is to create the object equivalent
Object
.
'use strict';
var foo = new String("my string");
foo.bar = {};