类型错误:"x" 不是非空对象
当某个地方需要一个对象但未提供时,就会发生 JavaScript 异常 "不是非空对象"。null
不是一个对象并且不会工作。
¥The JavaScript exception "is not a non-null object" occurs when an object is expected
somewhere and wasn't provided. null
is not an object and won't work.
信息
错误类型
什么地方出了错?
示例
预期属性描述符
¥Property descriptor expected
当使用 Object.create()
或 Object.defineProperty()
和 Object.defineProperties()
等方法时,可选描述符参数需要一个属性描述符对象。不提供任何对象(例如仅提供数字),将引发错误:
¥When methods like Object.create()
or
Object.defineProperty()
and Object.defineProperties()
are
used, the optional descriptor parameter expects a property descriptor object. Providing
no object (like just a number), will throw an error:
js
Object.defineProperty({}, "key", 1);
// TypeError: 1 is not a non-null object
Object.defineProperty({}, "key", null);
// TypeError: null is not a non-null object
有效的属性描述符对象可能如下所示:
¥A valid property descriptor object might look like this:
js
Object.defineProperty({}, "key", { value: "foo", writable: false });