TypeError: 需要更多参数
JavaScript 异常“需要更多参数”出现在函数调用方式错误时。需要提供更多参数。
消息
TypeError: Object prototype may only be an Object or null: undefined (V8-based) TypeError: Object.create requires at least 1 argument, but only 0 were passed (Firefox) TypeError: Object.setPrototypeOf requires at least 2 arguments, but only 0 were passed (Firefox) TypeError: Object.defineProperties requires at least 1 argument, but only 0 were passed (Firefox) TypeError: Object prototype may only be an Object or null. (Safari)
错误类型
哪里出错了?
函数调用方式错误。需要提供更多参数。
示例
未提供必需的参数
Object.create()
方法至少需要一个参数,而 Object.setPrototypeOf()
方法至少需要两个参数
js
const obj = Object.create();
// TypeError: Object.create requires at least 1 argument, but only 0 were passed
const obj2 = Object.setPrototypeOf({});
// TypeError: Object.setPrototypeOf requires at least 2 arguments, but only 1 were passed
您可以通过将 null
设置为原型来解决此问题,例如
js
const obj = Object.create(null);
const obj2 = Object.setPrototypeOf({}, null);
另请参阅
- 函数指南