-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
84 lines (64 loc) · 2.58 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: TypeError
slug: Web/JavaScript/Reference/Global_Objects/TypeError
---
{{JSRef}}
**`TypeError`**(类型错误)对象通常(但并不只是)用来表示值的类型非预期类型时发生的错误。
以下情况会抛出 `TypeError`:
- 传递给运算符的操作数或传递给函数的参数与预期的类型不兼容;或
- 尝试修改无法更改的值;或
- 尝试以不适当的方法使用一个值。
`TypeError` 是一个{{Glossary("serializable object", "可序列化对象")}},所以可以使用 {{domxref("structuredClone()")}} 对它进行克隆,也可以使用 {{domxref("Worker/postMessage()", "postMessage()")}} 在 [Worker](/zh-CN/docs/Web/API/Worker) 之间拷贝它。
## 构造函数
- {{jsxref("Global_Objects/TypeError/TypeError", "TypeError()")}}
- : 创建一个新的 `TypeError` 对象。
## 实例属性
- {{jsxref("Error.prototype.message")}}
- : 错误消息。继承自 {{jsxref("Error")}}。
- {{jsxref("Error.prototype.name")}}
- : 错误名称。继承自 {{jsxref("Error")}}。
- {{jsxref("Error.prototype.cause")}}
- : 表示导致当前错误被抛出的原因。继承自 {{jsxref("Error")}}。
- {{jsxref("Error.prototype.fileName")}} {{non-standard_inline}}
- : 引发此错误的文件的路径。继承自 {{jsxref("Error")}}。
- {{jsxref("Error.prototype.lineNumber")}} {{non-standard_inline}}
- : 引发此错误的代码所在的文件的行号。继承自 {{jsxref("Error")}}。
- {{jsxref("Error.prototype.columnNumber")}} {{non-standard_inline}}
- : 引发此错误的代码在文件中所在行的列号。继承自 {{jsxref("Error")}}。
- {{jsxref("Error.prototype.stack")}} {{non-standard_inline}}
- : 堆栈跟踪。继承自 {{jsxref("Error")}}。
## 示例
### 捕获类型错误
```js
try {
null.f();
} catch (e) {
console.log(e instanceof TypeError); // true
console.log(e.message); // "null has no properties"
console.log(e.name); // "TypeError"
console.log(e.fileName); // "Scratchpad/1"
console.log(e.lineNumber); // 2
console.log(e.columnNumber); // 2
console.log(e.stack); // "@Scratchpad/2:2:3\n"
}
```
### 创建一个类型错误
```js
try {
throw new TypeError("Hello", "someFile.js", 10);
} catch (e) {
console.log(e instanceof TypeError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "TypeError"
console.log(e.fileName); // "someFile.js"
console.log(e.lineNumber); // 10
console.log(e.columnNumber); // 0
console.log(e.stack); // "@Scratchpad/2:2:9\n"
}
```
## 规范
{{Specifications}}
## 浏览器兼容性
{{Compat}}
## 参见
- {{jsxref("Error")}}