此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
TypeError: "x" is not a non-null object
提示信息
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)
In this article
错误类型
TypeError哪里出错了?
在期待出现对象类型的值的地方而没有提供。null 不是对象类型,因此不起作用。必须在给定的场景下提供一个合适的对象。
示例
>期望的属性描述器
当使用诸如Object.create() 或Object.defineProperty() 及{jsxref("Object.defineProperties()")}} 方法时,可选的属性描述器参数需要提供一个描述器对象。提供非对象类型的值(例如数字)将会报错:
js
Object.defineProperty({}, "key", 1);// TypeError: 1 is not a non-null objectObject.defineProperty({}, "key", null);// TypeError: null is not a non-null object一个合法的描述器对象类似于下面这样:
js
Object.defineProperty({}, "key", { value: "foo", writable: false });WeakMap 和WeakSet 对象需要对象类型的键
WeakMap 和WeakSet 对象只能存储对象类型的键,而不能使用其他类型的。
js
var ws = new WeakSet();ws.add("foo");// TypeError: "foo" is not a non-null object用对象类型的值来替换:
js
ws.add({ foo: "bar" });ws.add(window);