Movatterモバイル変換


[0]ホーム

URL:


  1. Веб-технологии для разработчиков
  2. JavaScript
  3. Справочник по JavaScript
  4. JavaScript ссылки на ошибки
  5. TypeError: can't assign to property "x" on "y": not an object

This page was translated from English by the community.Learn more and join the MDN Web Docs community.

View in EnglishAlways switch to English

TypeError: can't assign to property "x" on "y": not an object

Ошибка строгого режима JavaScript "can't assign to property" ("невозможно присвоить свойство") происходит в тот момент, когда вы пытаетесь создать свойство примитивного типа данных (такого как символ, строка, число или булевое значение). Примитивные типы данных не могут содержать никаких свойств.

Message

TypeError: can't assign to property "x" on {y}: not an object> (Firefox)TypeError: Cannot create property 'x' on {y} (Chrome)

Error type

TypeError.

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

js
"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 object

Fixing 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.

js
"use strict";var foo = new String("my string");foo.bar = {};

Смотрите также

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp