You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Classes, functions, and object literals are namespaces
87
+
## 类、函数和对象字面量是命名空间
98
88
99
-
Classes are namespaces in `.js` files.
100
-
This can be used to nest classes, for example:
89
+
在 `.js` 文件中,类可以作为命名空间。这可以用于嵌套类,例如:
101
90
102
91
```js twoslash
103
92
classC {}
104
93
C.D=class {};
105
94
```
106
95
107
-
And, for pre-ES2015 code, it can be used to simulate static methods:
96
+
对于 ES2015 之前的代码,它可以用来模拟静态方法:
108
97
109
98
```js twoslash
110
99
functionOuter() {
@@ -118,7 +107,7 @@ Outer.Inner = function () {
118
107
Outer.Inner();
119
108
```
120
109
121
-
It can also be used to create simple namespaces:
110
+
它还可以用来创建简单的命名空间:
122
111
123
112
```js twoslash
124
113
var ns = {};
@@ -128,39 +117,36 @@ ns.func = function () {};
128
117
ns;
129
118
```
130
119
131
-
Other variants are allowed as well:
120
+
其他变体也是允许的:
132
121
133
122
```js twoslash
134
-
//IIFE
123
+
//自执行函数
135
124
var ns = (function (n) {
136
125
return n || {};
137
126
})();
138
127
ns.CONST=1;
139
128
140
-
//defaulting to global
129
+
//默认为全局
141
130
var assign =
142
131
assign ||
143
132
function () {
144
-
//code goes here
133
+
//代码在这里
145
134
};
146
135
assign.extra=1;
147
136
```
148
137
149
-
## Object literals are open-ended
138
+
## 对象字面量是开放式的
150
139
151
-
In a `.ts` file, an object literal that initializes a variable declaration gives its type to the declaration.
152
-
No new members can be added that were not specified in the original literal.
153
-
This rule is relaxed in a `.js` file; object literals have an open-ended type (an index signature) that allows adding and looking up properties that were not defined originally.
## Var-args parameter declaration inferred from use of `arguments`
213
+
## 从 `arguments` 的使用推断可变参数声明
232
214
233
-
A function whose body has a reference to the `arguments`reference is implicitly considered to have a var-arg parameter (i.e. `(...arg: any[]) => any`). Use JSDoc var-arg syntax to specify the type of the arguments.
this.props.b; //Allowed, since this.props is of type any
241
+
this.props.b; //允许,因为 this.props 的类型为 any
261
242
}
262
243
}
263
244
```
264
245
265
-
Use JSDoc `@augments`to specify the types explicitly. for instance:
246
+
使用 JSDoc 的 `@augments`来显式指定类型。例如:
266
247
267
248
```js
268
249
import { Component } from"react";
@@ -272,32 +253,32 @@ import { Component } from "react";
272
253
*/
273
254
classMyComponentextendsComponent {
274
255
render() {
275
-
this.props.b; //Error: b does not exist on {a:number}
256
+
this.props.b; //错误:b 在 {a:number} 上不存在
276
257
}
277
258
}
278
259
```
279
260
280
-
### In JSDoc references
261
+
### 在 JSDoc 引用中
281
262
282
-
An unspecified type argument in JSDoc defaults to any:
263
+
JSDoc 中未指定的类型参数默认值为 any:
283
264
284
265
```js twoslash
285
266
/**@type{Array} */
286
267
var x = [];
287
268
288
269
x.push(1); // OK
289
-
x.push("string"); // OK, x is of type Array<any>
270
+
x.push("string"); // OK,x 的类型为 Array<any>
290
271
291
272
/**@type{Array.<number>} */
292
273
var y = [];
293
274
294
275
y.push(1); // OK
295
-
y.push("string"); //Error, string is not assignable to number
276
+
y.push("string"); //错误,string 不能赋值给 number
296
277
```
297
278
298
-
### In function calls
279
+
### 在函数调用中
299
280
300
-
A call to a generic function uses the arguments to infer the type parameters. Sometimes this process fails to infer any types, mainly because of lack of inference sources; in these cases, the type parameters will default to `any`. For example:
0 commit comments