This page was translated from English by the community.Learn more and join the MDN Web Docs community.
TypeError: can't assign to property "x" on "y": not an object
Ошибка строгого режима JavaScript "can't assign to property" ("невозможно присвоить свойство") происходит в тот момент, когда вы пытаетесь создать свойство примитивного типа данных (такого как символ, строка, число или булевое значение). Примитивные типы данных не могут содержать никаких свойств.
In this article
Message
TypeError: can't assign to property "x" on {y}: not an object> (Firefox)TypeError: Cannot create property 'x' on {y} (Chrome)Error type
What went wrong?
InStrict_mode, aTypeError is raised when attempting to create a property onprimitive value such as asymbol, astring, anumber or aboolean.Primitive values cannot hold anyproperty.
The problem might be that an unexpected value is flowing at an unexpected place, or that an object variant of aString or aNumber is expected.
Examples
>Invalid cases
"use strict";var foo = "my string";// The following line does nothing if not in strict mode.foo.bar = {}; // TypeError: can't assign to property "bar" on "my string": not an objectFixing the issue
Either fix the code to prevent theprimitive from being used in such places, or fix the issue is to create the object equivalentObject.
"use strict";var foo = new String("my string");foo.bar = {};