TypeError
TypeError
对象表示无法执行操作时的错误,通常(但不限于)当值不是预期类型时。
¥The TypeError
object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
在以下情况下可能会抛出 TypeError
:
¥A TypeError
may be thrown when:
- 传递给函数的操作数或参数与该运算符或函数期望的类型不兼容;or
- 当尝试修改无法更改的值时;or
- 当试图以不适当的方式使用某个值时。
TypeError
是 serializable object,因此可以使用 structuredClone()
进行克隆,或者使用 postMessage()
在 工人 之间进行复制。
¥TypeError
is a serializable object, so it can be cloned with structuredClone()
or copied between Workers using postMessage()
.
TypeError
是 Error
的子类。
¥TypeError
is a subclass of Error
.
构造函数
实例属性
¥Instance properties
还从其父级 Error
继承实例属性。
¥Also inherits instance properties from its parent Error
.
这些属性在 TypeError.prototype
上定义并由所有 TypeError
实例共享。
¥These properties are defined on TypeError.prototype
and shared by all TypeError
instances.
TypeError.prototype.constructor
-
创建实例对象的构造函数。对于
TypeError
实例,初始值为TypeError
构造函数。 TypeError.prototype.name
-
表示错误类型的名称。对于
TypeError.prototype.name
,初始值为"TypeError"
。
实例方法
示例
捕获类型错误
创建类型错误
规范
Specification |
---|
ECMAScript Language Specification # sec-native-error-types-used-in-this-standard-typeerror |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also