TypeError: "x" is not a non-null object - JavaScript 编辑

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.

Message

TypeError: Invalid descriptor for property {x} (Edge)
TypeError: "x" is not a non-null object (Firefox)
TypeError: Property description must be an object: "x" (Chrome)
TypeError: Invalid value used in weak set (Chrome)

Error type

TypeError

What went wrong?

An object is expected somewhere and wasn't provided. null is not an object and won't work. You must provide a proper object in the given situation.

Examples

Property descriptor expected

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:

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:

Object.defineProperty({}, 'key', { value: 'foo', writable: false });

WeakMap and WeakSet objects require object keys

WeakMap and WeakSet objects store object keys. You can't use other types as keys.

var ws = new WeakSet();
ws.add('foo');
// TypeError: "foo" is not a non-null object

Use objects instead:

ws.add({foo: 'bar'});
ws.add(window);

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:132 次

字数:4020

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文