syntax-uts.md 95.3 KB
Newer Older
DCloud_Heavensoft's avatar
DCloud_Heavensoft 已提交
1
## UTS介绍
fxy060608's avatar
fxy060608 已提交
2 3 4

**uts 是什么**

W
wanganxp 已提交
5
uts,全称 uni type script,是一门跨平台的、高性能的、强类型的现代编程语言。
fxy060608's avatar
fxy060608 已提交
6

W
wanganxp 已提交
7 8 9 10
它可以被编译为不同平台的编程语言,如:
- web平台,编译为JavaScript
- Android平台,编译为Kotlin
- iOS平台,编译Swift
fxy060608's avatar
fxy060608 已提交
11

W
wanganxp 已提交
12
uts 采用了与 ts 基本一致的语法规范,支持绝大部分 ES6 API。
fxy060608's avatar
fxy060608 已提交
13

DCloud_Heavensoft's avatar
DCloud_Heavensoft 已提交
14 15 16 17
但为了跨端,uts进行了一些约束和特定平台的增补。

过去在js引擎下运行支持的语法,大部分在uts的处理下也可以平滑的在kotlin和swift中使用。但有一些无法抹平,需要使用条件编译。和uni-app的条件编译类似,uts也支持条件编译。写在条件编译里的,可以调用平台特有的扩展语法。

W
wanganxp 已提交
18
本文是 uts 的基本语法介绍。如想了解 uni-app 下如何开发 uts插件,另见文档[https://uniapp.dcloud.net.cn/plugin/uts-plugin.html](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
fxy060608's avatar
fxy060608 已提交
19

DCloud_Heavensoft's avatar
DCloud_Heavensoft 已提交
20

fxy060608's avatar
fxy060608 已提交
21 22
## 基本语法
### 声明
fxy060608's avatar
fxy060608 已提交
23

W
wanganxp 已提交
24 25 26
js是无类型的,TypeScript 的 type 就是类型的意思,给js加上了类型。它的类型定义方式是在变量名后面通过加冒号和类型来进行定义。

uts 中声明变量可以用 let 或 const,详见下。
fxy060608's avatar
fxy060608 已提交
27

W
wanganxp 已提交
28
#### 变量定义(let)
fxy060608's avatar
fxy060608 已提交
29

D
DCloud_LXH 已提交
30
声明一个可重新赋值的变量。语法 `let [变量名] : [类型] = 值;`
fxy060608's avatar
fxy060608 已提交
31

D
DCloud_LXH 已提交
32
> 相当于 TypeScript 中的 let,kotlin 中的 var
fxy060608's avatar
fxy060608 已提交
33 34

```ts
W
wanganxp 已提交
35
let str :string = "hello"; // 声明一个字符串变量
fxy060608's avatar
fxy060608 已提交
36 37 38
str = "hello world"; // 重新赋值
```

W
wanganxp 已提交
39 40
类型除了 string 之外,更多类型[见下](#基本类型)

W
wanganxp 已提交
41
#### 常量定义(const)
fxy060608's avatar
fxy060608 已提交
42

D
DCloud_LXH 已提交
43
声明一个只读常量,只能为其赋值一次。语法 `const [变量名] : [类型] = 值;`
fxy060608's avatar
fxy060608 已提交
44

D
DCloud_LXH 已提交
45
> 相当于 TypeScript 中的 const, kotlin 中的 val
fxy060608's avatar
fxy060608 已提交
46 47

```ts
W
wanganxp 已提交
48
const str :string = "hello"; // 声明一个字符串变量
fxy060608's avatar
fxy060608 已提交
49 50 51
str = "hello world"; // 报错,不允许重新赋值
```

fxy060608's avatar
fxy060608 已提交
52 53
注意事项:

W
wanganxp 已提交
54 55 56 57
- 当前 uts 并未限制使用 var 来声明变量,但当使用 var 来声明变量时需要注意不同平台差异
	* 编译至 JavaScript 平台时,等同于 JavaScript 平台的 var (存在变量提升现象)
	* 编译至 Kotlin 平台时,等同于 Kotlin 平台的 var(允许重新赋值)
- 类型定义的冒号,左右可以有一个空格,也可以没有空格。`let str:string``let str : string``let str :string``let str: string` 都是合法的。
fxy060608's avatar
fxy060608 已提交
58
- 不支持 TypeScript 中的联合类型
fxy060608's avatar
fxy060608 已提交
59

W
wanganxp 已提交
60
#### 变量命名规则
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69

在 uts 中,使用变量名需要遵守一定的规则。

-   变量名称可以包含数字和字母。
-   除了下划线 \_ 外,不能包含其他特殊字符,包括空格。
-   变量名不能以数字开头。

> 注意:与 TypeScript 不同的是,uts 不允许以 $ 开头命名变量

W
wanganxp 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#### 类型自动推导

uts具备类型自动推导。在定义变量时如果直接赋值,而不使用冒号定义类型,也可以合法运行。

如下2种写法都是合法的,两个变量都是string类型:

```ts
let s1 :string = "hello"; 
let s2 = "hello"; 
```

#### any类型

如果定义变量时没有声明类型,也没有赋值。那么这个变量会被视为any类型。虽然可以使用,但uts中非常不建议这样使用。

```ts
let s;
s = "123"
console.log(s) // hello world
```

fxy060608's avatar
fxy060608 已提交
91
### 操作符
fxy060608's avatar
fxy060608 已提交
92

fxy060608's avatar
fxy060608 已提交
93
#### 赋值运算符(Assignment operators)
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

| 名字                                              | 简写的操作符 | 含义        |
| ------------------------------------------------- | ------------ | ----------- | ---------- |
| 赋值(Assignment)                                  | x = y        | x = y       |
| 加法赋值(Addition assignment)                     | x += y       | x = x + y   |
| 减法赋值(Subtraction assignment)                  | x -= y       | x = x - y   |
| 乘法赋值(Multiplication assignment)               | x \*= y      | x = x \* y  |
| 除法赋值(Division assignment)                     | x /= y       | x = x / y   |
| 求余赋值(Remainder assignment)                    | x %= y       | x = x % y   |
| 左移位赋值(Left shift assignment)                 | x <<= y      | x = x << y  |
| 右移位赋值(Right shift assignment)                | x >>= y      | x = x >> y  |
| 无符号右移位赋值(Unsigned right shift assignment) | x >>>= y     | x = x >>> y |
| 按位与赋值(Bitwise AND assignment)                | x &= y       | x = x & y   |
| 按位异或赋值(Bitwise XOR assignment)              | x ^= y       | x = x ^ y   |
| 按位或赋值(Bitwise OR assignment)                 | x \|= y      | x \|= y     | x = x \| y |

fxy060608's avatar
fxy060608 已提交
110
#### 比较运算符(Comparison operators)
fxy060608's avatar
fxy060608 已提交
111 112 113 114 115 116 117 118 119 120 121 122

| 运算符                              | 描述                                        | 返回 true 的示例 |
| ----------------------------------- | ------------------------------------------- | ---------------- |
| 等于 Equal (==)                     | 如果两边操作数相等时返回 true。             | var1==var2       |
| 不等于 Not equal (!=)               | 如果两边操作数不相等时返回 true             | var1!=var2       |
| 全等 Strict equal (===)             | 两边操作数相等且类型相同时返回 true。       | var1===var2      |
| 不全等 Strict not equal (!==)       | 两边操作数不相等或类型不同时返回 true。     | var1!==var2      |
| 大于 Greater than (>)               | 左边的操作数大于右边的操作数返回 true       | var1>var2        |
| 大于等于 Greater than or equal (>=) | 左边的操作数大于或等于右边的操作数返回 true | var1>=var2       |
| 小于 Less than (<)                  | 左边的操作数小于右边的操作数返回 true       | var1<var2        |
| 小于等于 Less than or equal (<=)    | 左边的操作数小于或等于右边的操作数返回 true | var1<=var2       |

fxy060608's avatar
fxy060608 已提交
123
#### 算数运算符(Arithmetic operators)
fxy060608's avatar
fxy060608 已提交
124 125 126 127 128 129 130

| 运算符   | 范例 | 描述                                                                                                                                     |
| -------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| 求余(%)  |      | 二元运算符. 返回相除之后的余数.                                                                                                          |
| 自增(++) |      | 一元运算符. 将操作数的值加一. 如果放在操作数前面 (++x), 则返回加一后的值; 如果放在操作数后面 (x++), 则返回操作数原值,然后再将操作数加一. |
| 自减(--) |      | 一元运算符. 将操作数的值减一. 前后缀两种用法的返回值类似自增运算符.                                                                      |

fxy060608's avatar
fxy060608 已提交
131
#### 位运算符(Bitwise operators)
fxy060608's avatar
fxy060608 已提交
132 133 134 135 136 137 138 139 140 141 142

| Operator                        | Usage   | Description                                                                                                      |
| ------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------- |
| 按位与 AND                      | a & b   | 在 a,b 的位表示中,每一个对应的位都为 1 则返回 1, 否则返回 0.                                                   |
| 按位或 OR                       | a \| b  | 在 a,b 的位表示中,每一个对应的位,只要有一个为 1 则返回 1, 否则返回 0.                                         |
| 按位异或 XOR                    | a ^ b   | 在 a,b 的位表示中,每一个对应的位,两个不相同则返回 1,相同则返回 0.                                             |
| 按位非 NOT                      | ~ a     | 反转被操作数的位。                                                                                               |
| 左移 shift                      | a << b  | 将 a 的二进制串向左移动 b 位,右边移入 0.                                                                         |
| 算术右移                        | a >> b  | 把 a 的二进制表示向右移动 b 位,丢弃被移出的所有位.(译注:算术右移左边空出的位是根据最高位是 0 和 1 来进行填充的) |
| 无符号右移(左边空出位用 0 填充) | a >>> b | 把 a 的二进制表示向右移动 b 位,丢弃被移出的所有位,并把左边空出的位都填充为 0                                   |

fxy060608's avatar
fxy060608 已提交
143
#### 逻辑运算符(Logical operators)
fxy060608's avatar
fxy060608 已提交
144 145 146 147 148 149 150

| 运算符       | 范例             | 描述     |
| ------------ | ---------------- | -------- |
| 逻辑与(&&)   | expr1 && expr2   | (逻辑与) |
| 逻辑或(\|\|) | expr1 \|\| expr2 | (逻辑或) |
| 逻辑非(!)    | !expr            | (逻辑非) |

fxy060608's avatar
fxy060608 已提交
151
#### 字符串运算符(String operators)
fxy060608's avatar
fxy060608 已提交
152 153 154 155 156 157 158

除了比较操作符,它可以在字符串值中使用,连接操作符(+)连接两个字符串值相连接,返回另一个字符串,它是两个操作数串的结合。

```ts
console.log("my " + "string"); // console logs the string "my string".
```

fxy060608's avatar
fxy060608 已提交
159
#### 条件(三元)运算符(Conditional operator)
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167 168

条件运算符是 uts 中唯一需要三个操作数的运算符。运算的结果根据给定条件在两个值中取其一。语法为:

`条件 ? 值1 : 值2`

```ts
const status = age >= 18 ? "adult" : "minor";
```

W
wanganxp 已提交
169 170 171 172 173 174 175 176
### 代码语句的分割

uts的多个代码语句,可以以回车或分号分割。行尾的分号可以省略。如果写在一行,应以分号分割。

如下的代码都是合法的:

```ts
let a:number = 1 //行尾可以不加分号
W
wanganxp 已提交
177 178
let b:boolean = false; //行尾可以加分号
let c:number = 3 ; let d:number = 4 // 同行多语句需要用分号分割
W
wanganxp 已提交
179 180
```

fxy060608's avatar
fxy060608 已提交
181
## 数据类型
fxy060608's avatar
fxy060608 已提交
182

fxy060608's avatar
fxy060608 已提交
183
### 布尔值(Boolean)
fxy060608's avatar
fxy060608 已提交
184

W
wanganxp 已提交
185
有 2 个值分别是:`true``false`
fxy060608's avatar
fxy060608 已提交
186

fxy060608's avatar
fxy060608 已提交
187
### 数字(Number)
fxy060608's avatar
fxy060608 已提交
188

W
wanganxp 已提交
189 190 191 192 193 194
所有数字,包括整数或浮点数,包括正数负数。例如: 正整数 `42` 或者 浮点数 `3.14159` 或者 负数 `-1`

```ts
let a:number = 42
```

fxy060608's avatar
fxy060608 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#### Kotlin 特有的数字类型

- Byte, UByte
- Short, UShort
- Int, UInt
- Long, ULong
- Float
- Double

#### Swift 特有的数字类型

- Int, UInt, Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64
- Float, Float16, Float32, Float64
- Double

**注意**

在 kotlin 和 swift 中,有些系统API或三方SDK的传入和返回强制约定了这些平台数字类型,此时无法使用 number。
W
wanganxp 已提交
213
这种情况下可以使用下面的方法,虽然可能会被编辑器报语法错误(后续HBuilderX会修复这类误报),但编译到 kotlin 和 swift 时是可用的。
fxy060608's avatar
fxy060608 已提交
214 215

- 声明特定的平台数字类型
fxy060608's avatar
fxy060608 已提交
216 217
 > 目前这些平台数字类型,声明类型时,与 number 不同的是,均为首字母大写

W
wanganxp 已提交
218
```ts
fxy060608's avatar
fxy060608 已提交
219 220 221

let a:Int = 3 //注意 Int 是首字母大写
let b:Int = 4
W
wanganxp 已提交
222 223
let c:Double  = a * 1.0 / b
```
fxy060608's avatar
fxy060608 已提交
224

fxy060608's avatar
fxy060608 已提交
225 226 227 228 229 230 231 232 233 234 235 236
- 在 kotlin(app-android) 下转换特定的平台数字类型
```ts
let a:Int = 3
a.toFloat() // 转换为 Float 类型,后续也将支持 new Float(a) 方式转换
a.toDouble() // 转换为 Double 类型,后续也将支持 new Double(a) 方式转换
```

- 在 swift(app-ios) 下转换特定的平台数字类型
```ts
let a:Int = 3
let b = new Double(a) // 将整型变量 a 转换为 Double 类型
```
Y
yurj26 已提交
237 238 239 240 241 242
注意事项:

- 在不同平台上,数值的范围限制不同,超出限制会导致相应的错误或异常
	* 编译至 JavaScript 平台时,最大值为 1.7976931348623157e+308,最小值为 -1.7976931348623157e+308,超出限制会返回 Infinity 或 -Infinity。
	* 编译至 Kotlin 平台时,最大值为 9223372036854775807,最小值为 -9223372036854775808,超出限制会报错:`The value is out of range‌`
  * 编译至 Swift 平台时,最大值 9223372036854775807,最小值 -9223372036854775808,超出限制会报错:`integer literal overflows when stored into Int`
fxy060608's avatar
fxy060608 已提交
243

fxy060608's avatar
fxy060608 已提交
244
### 字符串(String)
fxy060608's avatar
fxy060608 已提交
245

W
wanganxp 已提交
246 247 248 249 250 251
字符串是一串表示文本值的字符序列,例如:`"hello world"`

### 日期(Date)

日期对象表示日期,包括年月日时分秒等各种日期。详[见下](#Date)

fxy060608's avatar
fxy060608 已提交
252
### null
fxy060608's avatar
fxy060608 已提交
253

fxy060608's avatar
fxy060608 已提交
254
一个表明 null 值的特殊关键字。
fxy060608's avatar
fxy060608 已提交
255

W
wanganxp 已提交
256 257 258 259 260 261 262
有时需定义可为null的字符串,可以在类型描述中使用`|`操作符。
```ts
let user: string | null
```

> 注意:uts 编译为kotlin和swift时不支持 undefined。

fxy060608's avatar
fxy060608 已提交
263 264 265 266
### Object类型

对象(object)是指内存中的可以被标识符引用的一块区域,是一种引用类型。包括Array,Date,Map,Set,JSON等,uts 有一个内置对象的标准库。详[见下](#内置对象和api)

W
wanganxp 已提交
267 268 269 270
### any类型

未定义类型,即任意类型。一般不推荐使用。

fxy060608's avatar
fxy060608 已提交
271
## 字面量
fxy060608's avatar
fxy060608 已提交
272

W
wanganxp 已提交
273
字面量是由语法表达式定义的常量;或,通过由一定字词组成的语词表达式定义的常量。
fxy060608's avatar
fxy060608 已提交
274 275 276

在 uts 中,你可以使用各种字面量。这些字面量是按字面意思给出的固定的值,而不是变量

fxy060608's avatar
fxy060608 已提交
277
### 数组字面量
fxy060608's avatar
fxy060608 已提交
278 279 280 281 282 283 284 285 286 287 288

数组字面值是一个封闭在方括号对 ([]) 中的包含有零个或多个表达式的列表,其中每个表达式代表数组的一个元素。当你使用数组字面值创建一个数组时,该数组将会以指定的值作为其元素进行初始化,而其长度被设定为元素的个数。

下面的示例用 3 个元素生成数组coffees,它的长度是 3。

```ts
const coffees = ["French Roast", "Colombian", "Kona"]
```

数组字面值同时也是数组对象。

fxy060608's avatar
fxy060608 已提交
289
### 布尔字面量
fxy060608's avatar
fxy060608 已提交
290 291 292

布尔类型有两种字面量:true和false。

fxy060608's avatar
fxy060608 已提交
293
### 数字字面量
fxy060608's avatar
fxy060608 已提交
294 295 296

数字字面量包括多种基数的整数字面量和以 10 为基数的浮点数字面量

fxy060608's avatar
fxy060608 已提交
297
#### 整数字面量
fxy060608's avatar
fxy060608 已提交
298 299 300 301 302 303 304 305 306

整数可以用十进制(基数为 10)、十六进制(基数为 16)、二进制(基数为 2)表示。

- 十进制整数字面量由一串数字序列组成,且没有前缀 0。如:`0, 117, -345`

- 十六进制整数以 0x(或 0X)开头,可以包含数字(0-9)和字母 a~f 或 A~F。如:`0x1123, 0x00111 , -0xF1A7`

- 二进制整数以 0b(或 0B)开头,只能包含数字 0 和 1。如:`0b11, 0b0011 , -0b11`

fxy060608's avatar
fxy060608 已提交
307
#### 浮点数字面量
fxy060608's avatar
fxy060608 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

浮点数字面值可以有以下的组成部分:

- 一个十进制整数,可以带正负号(即前缀“+”或“ - ”),
- 小数点(“.”),
- 小数部分(由一串十进制数表示),
- 指数部分。

指数部分以“e”或“E”开头,后面跟着一个整数,可以有正负号(即前缀“+”或“-”)。浮点数字面量至少有一位数字,而且必须带小数点或者“e”(大写“E”也可)。

简言之,其语法是:

```
[(+|-)][digits][.digits][(E|e)[(+|-)]digits]
```

例如:

```ts
3.14
-.2345789 // -0.23456789
-3.12e+12  // -3.12*10^12
.1e-23    // 0.1*10^(-23)=10^(-24)=1e-24
```

fxy060608's avatar
fxy060608 已提交
333
### RegExp字面量
fxy060608's avatar
fxy060608 已提交
334 335 336 337 338 339 340

正则表达式是字符被斜线围成的表达式。下面是一个正则表达式文字的一个例子。

```ts
const re = /ab+c/;
```

fxy060608's avatar
fxy060608 已提交
341
### 字符串字面量
fxy060608's avatar
fxy060608 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

字符串字面量是由双引号(")对或单引号(')括起来的零个或多个字符。字符串被限定在同种引号之间;也即,必须是成对单引号或成对双引号。下面的例子都是字符串字面值:

```ts
"foo"
'bar'
"1234"
"one line \n another line"
"John's cat"
```

你可以在字符串字面值上使用字符串对象的所有方法,你也能用对字符串字面值使用类似 String.length 的属性:

```ts
console.log("John's cat".length)
// 将打印字符串中的字符个数(包括空格)
// 结果为:10
```

fxy060608's avatar
fxy060608 已提交
361
#### 模板字符串
fxy060608's avatar
fxy060608 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

模板字面量 是允许嵌入表达式的字符串字面量。你可以使用多行字符串和字符串插值功能。也被称为“模板字符串”。

```ts
// Basic literal string creation
`In uts '\n' is a line-feed.`

// Multiline strings
`In uts this is
 not legal.`

// String interpolation
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`
```
fxy060608's avatar
fxy060608 已提交
377
#### 转义特殊字符
fxy060608's avatar
fxy060608 已提交
378 379 380 381 382 383 384 385 386 387 388 389

|字符	|意思		|
|--		|--			|
|`\b`	|退格符		|
|`\f`	|换页符		|
|`\n`	|换行符		|
|`\r`	|回车符		|
|`\t`	|制表符		|
|`\'`	|单引号		|
|`\"`	|双引号		|
|`\\`	|反斜杠字符	|

fxy060608's avatar
fxy060608 已提交
390
## 控制流程
fxy060608's avatar
fxy060608 已提交
391

fxy060608's avatar
fxy060608 已提交
392
### 条件
fxy060608's avatar
fxy060608 已提交
393

fxy060608's avatar
fxy060608 已提交
394
#### If 语句
fxy060608's avatar
fxy060608 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

当一个逻辑条件为真,用 if 语句执行一个语句。当这个条件为假,使用可选择的 else 从句来执行这个语句。if 语句如下所示:

```ts
if (condition_1) {
    statement_1;
} else if (condition_2) {
    statement_2;
} else if (condition_n_1) {
    statement_n;
} else {
    statement_last;
}
```

> 注意:if 和 else if 中的条件表达式必须为布尔值

fxy060608's avatar
fxy060608 已提交
412
#### switch 语句
fxy060608's avatar
fxy060608 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

switch 语句允许一个程序求一个表达式的值并且尝试去匹配表达式的值到一个 case 标签。如果匹配成功,这个程序执行相关的语句。switch 语句如下所示:

```ts
switch (expression) {
   case label_1:
      statements_1
      [break;]
   case label_2:
      statements_2
      [break;]
   default:
      statements_def
      [break;]
}
```

程序首先查找一个与 expression 匹配的 case 语句,然后将控制权转移到该子句,执行相关的语句。如果没有匹配值, 程序会去找 default 语句,如果找到了,控制权转移到该子句,执行相关的语句。如果没有找到 default,程序会继续执行 switch 语句后面的语句。default 语句通常出现在 switch 语句里的最后面,当然这不是必须的。

可选的 break 语句与每个 case 语句相关联, 保证在匹配的语句被执行后程序可以跳出 switch 并且继续执行 switch 后面的语句。如果 break 被忽略,则程序将继续执行 switch 语句中的下一条语句。

fxy060608's avatar
fxy060608 已提交
434
#### 三元表达式
fxy060608's avatar
fxy060608 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

uts 支持使用三元表达式。一个条件后面会跟一个问号(?),如果条件为 true ,则问号后面的表达式 A 将会执行;表达式 A 后面跟着一个冒号(:),如果条件为 false ,则冒号后面的表达式 B 将会执行。本运算符经常作为 if 语句的简捷形式来使用。

```ts
function getFee(isMember: boolean): string {
    return isMember ? "$2.00" : "$10.00";
}

console.log(getFee(true));
// expected output: "$2.00"

console.log(getFee(false));
// expected output: "$10.00"

console.log(getFee(null));
// expected output: "$10.00"
```

三元操作符是右结合的,也就是说你可以像这样把它链接起来, 和 if … else if … else if … else 链类似:

```ts
function example(): string {
    return condition1
        ? value1
        : condition2
        ? value2
        : condition3
        ? value3
        : value4;
}

// Equivalent to:

function example(): string {
    if (condition1) {
        return value1;
    } else if (condition2) {
        return value2;
    } else if (condition3) {
        return value3;
    } else {
        return value4;
    }
}
```

fxy060608's avatar
fxy060608 已提交
481
### 循环
fxy060608's avatar
fxy060608 已提交
482

fxy060608's avatar
fxy060608 已提交
483
#### for
fxy060608's avatar
fxy060608 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

一个 for 循环会一直重复执行,直到指定的循环条件为 false。 一个 for 语句是这个样子的:

```ts
for ([initialExpression]; [condition]; [incrementExpression]) {
    statement;
}
```

当一个 for 循环执行的时候,会发生以下过程:

1. 如果有初始化表达式 initialExpression,它将被执行。这个表达式通常会初始化一个或多个循环计数器。
2. 计算 condition 表达式的值。如果 condition 的值是 true,循环中的语句会被执行。如果 condition 的值是 false,for 循环终止。如果 condition 表达式整个都被省略掉了,3. condition 的值会被认为是 true。
3. 循环中的 statement 被执行。如果需要执行多条语句,可以使用块({ ... })来包裹这些语句。
4. 如果有更新表达式 incrementExpression,执行更新表达式。
5. 回到步骤 2。

举例:

```ts
for (let i = 0; i < 10; i++) {
    //...
}
```
fxy060608's avatar
fxy060608 已提交
508
#### do...while
fxy060608's avatar
fxy060608 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

do...while 语句一直重复直到指定的条件求值得到假值(false)。 一个 do...while 语句看起来像这样:

```ts
do {
    statement;
} while (condition);
```

statement 在检查条件之前会执行一次。要执行多条语句(语句块),要使用块语句({ ... })包括起来。 如果 condition 为真(true),statement 将再次执行。 在每个执行的结尾会进行条件的检查。当 condition 为假(false),执行会停止并且把控制权交回给 do...while 后面的语句。

举例:

```ts
let i = 0;
do {
    i += 1;
} while (i < 10);
```

fxy060608's avatar
fxy060608 已提交
529
#### while
fxy060608's avatar
fxy060608 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

一个 while 语句只要指定的条件求值为真(true)就会一直执行它的语句块。一个 while 语句看起来像这样:

```ts
while (condition) {
    statement;
}
```

如果这个条件变为假,循环里的 statement 将会停止执行并把控制权交回给 while 语句后面的代码。

条件检测会在每次 statement 执行之前发生。如果条件返回为真, statement 会被执行并紧接着再次测试条件。如果条件返回为假,执行将停止并把控制权交回给 while 后面的语句。

要执行多条语句(语句块),要使用语句块 ({ ... }) 包括起来。

举例:

```ts
let n = 0;
let x = 0;
while (n < 3) {
    n++;
    x += n;
}
```

fxy060608's avatar
fxy060608 已提交
556
#### break
fxy060608's avatar
fxy060608 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

使用 break 语句来终止循环,switch。

举例:

```ts
for (let i = 0; i < 10; i++) {
    if (i > 5) {
        break;
    }
}
let x = 0;
while (true) {
    x++;
    if (x > 5) {
        break;
    }
}
```

fxy060608's avatar
fxy060608 已提交
577
#### continue
fxy060608's avatar
fxy060608 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

使用 continue 语句来终止当前循环,并在下一次迭代时继续执行循环。

举例:

```ts
for (let i = 0; i < 10; i++) {
    if (i > 5) {
        continue;
    }
}
let x = 0;
while (true) {
    x++;
    if (x > 5) {
        continue;
    }
}
```

fxy060608's avatar
fxy060608 已提交
598
### 异常
fxy060608's avatar
fxy060608 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

你可以用 throw 语句抛出一个异常并且用 try...catch 语句捕获处理它。

使用 throw 表达式来抛出异常:

```ts
throw new Error("Hi There!");
```

使用 try……catch 表达式来捕获异常:

```ts

try {
    // 一些代码
} catch (e: Error) {
    // 处理程序
} finally {
    // 可选的 finally 块
}

```

W
wanganxp 已提交
622
## 函数(function)
fxy060608's avatar
fxy060608 已提交
623

W
wanganxp 已提交
624 625 626
函数是编程语言常见的功能,它可以封装一批代码,对外接收参数,然后返回值。被封装的逻辑,可以被不同的其他代码调用,达到共同复用逻辑的目的。

函数用 function 关键字定义,后面跟着函数名和圆括号。
fxy060608's avatar
fxy060608 已提交
627

W
wanganxp 已提交
628
同时注意,定义函数涉及作用域。
fxy060608's avatar
fxy060608 已提交
629

fxy060608's avatar
fxy060608 已提交
630
### 定义函数
fxy060608's avatar
fxy060608 已提交
631

W
wanganxp 已提交
632
#### 普通函数声明
fxy060608's avatar
fxy060608 已提交
633

W
wanganxp 已提交
634
一个函数定义(也称为函数声明,或函数语句)由一系列在 function 关键字后的内容组成,依次为:
fxy060608's avatar
fxy060608 已提交
635 636 637 638 639 640 641 642

-   函数的名称。
-   函数参数列表,包围在括号中并由逗号分隔。
-   函数返回值类型。
-   定义函数的 uts 语句,用大括号{}括起来。

> 注意:函数必须明确标明返回值类型

W
wanganxp 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656
例如,以下的代码定义了一个简单的函数。函数名为 add,有2个参数 x 和 y,都是 string类型,函数的返回值类型也是 string。

函数的内容是将入参 x 和 y 相加,赋值给变量z,然后通过 return关键字返回z。

```ts
function add(x :string, y :string) :string {
    let z : string = x + " " + y
	return z;
}
```

#### 无返回值的函数定义(void)

如果这个函数不需要返回值,需要使用void关键字,同时函数内部末尾不需要return来返回内容。
fxy060608's avatar
fxy060608 已提交
657 658

```ts
W
wanganxp 已提交
659 660 661 662
function add(x :string, y :string) :void {
    let z :string = x + " " + y
	console.log(z)
	// 不需要return
fxy060608's avatar
fxy060608 已提交
663 664 665
}
```

W
wanganxp 已提交
666
#### 函数表达式和匿名函数定义
fxy060608's avatar
fxy060608 已提交
667

W
wanganxp 已提交
668
虽然上面的函数声明在语法上是一个语句,但函数也可以由函数表达式创建。这样的函数可以是匿名的,它不必有一个名称。例如,函数 add 也可这样来定义:
fxy060608's avatar
fxy060608 已提交
669 670 671

```ts
const add = function (x: string, y: string): string {
W
wanganxp 已提交
672
    return x + " " + y;
fxy060608's avatar
fxy060608 已提交
673 674 675
};
```

W
wanganxp 已提交
676 677 678
注意:
- 通过表达式定义的函数必须使用return关键字返回内容。
- 函数表达式不支持使用函数名,比如`const add = function add(){}`是不允许的。
fxy060608's avatar
fxy060608 已提交
679

fxy060608's avatar
fxy060608 已提交
680
### 调用函数
fxy060608's avatar
fxy060608 已提交
681

W
wanganxp 已提交
682 683 684
定义一个函数并不会自动的执行它。定义了函数仅仅是赋予函数以名称并明确函数被调用时该做些什么。调用函数才会以给定的参数真正执行这些动作。

定义了函数 add 后,你可以如下这样调用它:
fxy060608's avatar
fxy060608 已提交
685 686

```ts
W
wanganxp 已提交
687 688 689 690 691
function add(x :string, y :string) :string {
    let z :string = x + " " + y
	return z;
}
add("hello", "world"); // 调用add函数
fxy060608's avatar
fxy060608 已提交
692 693
```

W
wanganxp 已提交
694 695 696 697 698 699 700 701 702 703 704
上述语句通过提供参数 "hello" 和 "world" 来调用函数。

虽然调用了add函数,但并没有获取到返回值。如需要获取返回值,需要再赋值:
```ts
function add(x :string, y :string) :string {
	let z :string = x + " " + y
	return z;
}
let s :string = add("hello", "world");
console.log(s) // hello world
```
fxy060608's avatar
fxy060608 已提交
705

fxy060608's avatar
fxy060608 已提交
706
### 函数作用域
fxy060608's avatar
fxy060608 已提交
707 708 709 710

在函数内定义的变量不能在函数之外的任何地方访问,因为变量仅仅在该函数的域的内部有定义。相对应的,一个函数可以访问定义在其范围内的任何变量和函数。

```ts
W
wanganxp 已提交
711 712
const hello :string = "hello";
const world :string = "world";
fxy060608's avatar
fxy060608 已提交
713 714

function add(): string {
W
wanganxp 已提交
715
	let s1 :string = "123";
fxy060608's avatar
fxy060608 已提交
716 717 718 719
    return hello + world; // 可以访问到 hello 和 world
}
```

fxy060608's avatar
fxy060608 已提交
720
#### 嵌套函数
fxy060608's avatar
fxy060608 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

你可以在一个函数里面嵌套另外一个函数。嵌套(内部)函数对其容器(外部)函数是私有的。它自身也形成了一个闭包。一个闭包是一个可以自己拥有独立的环境与变量的表达式(通常是函数)。

既然嵌套函数是一个闭包,就意味着一个嵌套函数可以”继承“容器函数的参数和变量。换句话说,内部函数包含外部函数的作用域。

可以总结如下:

-   内部函数只可以在外部函数中访问。
-   内部函数形成了一个闭包:它可以访问外部函数的参数和变量,但是外部函数却不能使用它的参数和变量。

举例:

```ts
function addSquares(a: number, b: number): number {
    function square(x: number): number {
        return x * x;
    }
    return square(a) + square(b);
}
addSquares(2, 3); // returns 13
addSquares(3, 4); // returns 25
addSquares(4, 5); // returns 41
```

fxy060608's avatar
fxy060608 已提交
745
#### 命名冲突
fxy060608's avatar
fxy060608 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

当同一个闭包作用域下两个参数或者变量同名时,就会产生命名冲突。更近的作用域有更高的优先权,所以最近的优先级最高,最远的优先级最低。这就是作用域链。链的第一个元素就是最里面的作用域,最后一个元素便是最外层的作用域。

举例:

```ts
function outside(): (x: number) => number {
    let x = 5;
    const inside = function (x: number): number {
        return x * 2;
    };
    return inside;
}

outside()(10); // 返回值为 20 而不是 10
```

命名冲突发生在 return x 上,inside 的参数 x 和 outside 变量 x 发生了冲突。这里的作用链域是{inside, outside}。因此 inside 的 x 具有最高优先权,返回了 20(inside 的 x)而不是 10(outside 的 x)。

fxy060608's avatar
fxy060608 已提交
765
### 闭包
fxy060608's avatar
fxy060608 已提交
766

W
wanganxp 已提交
767
uts 允许函数嵌套,并且内部函数可以访问定义在外部函数中的所有变量和函数,以及外部函数能访问的所有变量和函数。
fxy060608's avatar
fxy060608 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

但是,外部函数却不能够访问定义在内部函数中的变量和函数。这给内部函数的变量提供了一定的安全性。

此外,由于内部函数可以访问外部函数的作用域,因此当内部函数生存周期大于外部函数时,外部函数中定义的变量和函数的生存周期将比内部函数执行时间长。当内部函数以某一种方式被任何一个外部函数作用域访问时,一个闭包就产生了。

举例:

```ts
const pet = function (name: string): () => string {
    //外部函数定义了一个变量"name"
    const getName = function (): string {
        //内部函数可以访问 外部函数定义的"name"
        return name;
    };
    //返回这个内部函数,从而将其暴露在外部函数作用域
    return getName;
};
const myPet = pet("Vivie");
myPet(); // 返回结果 "Vivie"
```
fxy060608's avatar
fxy060608 已提交
788
### 函数参数
fxy060608's avatar
fxy060608 已提交
789

fxy060608's avatar
fxy060608 已提交
790
#### 默认参数
fxy060608's avatar
fxy060608 已提交
791 792 793 794 795 796 797 798 799

函数参数可以有默认值,当省略相应的参数时使用默认值。

```ts
function multiply(a:number, b:number = 1):number {
  return a*b;
}
multiply(5); // 5
```
fxy060608's avatar
fxy060608 已提交
800
### 箭头函数
fxy060608's avatar
fxy060608 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813

箭头函数表达式(也称胖箭头函数)相比函数表达式具有较短的语法。箭头函数总是匿名的。

```ts
const arr = ["Hydrogen", "Helium", "Lithium", "Beryllium"];
const a2 = arr.map(function (s): number {
    return s.length;
});
console.log(a2); // logs [ 8, 6, 7, 9 ]
const a3 = arr.map((s): number => s.length);
console.log(a3); // logs [ 8, 6, 7, 9 ]
```

W
wanganxp 已提交
814 815 816
## 类(class)

uts 中使用关键字 class 声明类。
fxy060608's avatar
fxy060608 已提交
817

W
wanganxp 已提交
818
类声明由类名以及由花括号包围的类体构成。
fxy060608's avatar
fxy060608 已提交
819 820

```ts
W
wanganxp 已提交
821
// 定义Person Class
fxy060608's avatar
fxy060608 已提交
822
class Person {
W
wanganxp 已提交
823
	
fxy060608's avatar
fxy060608 已提交
824 825 826
}
```

W
wanganxp 已提交
827
### 基本概念
fxy060608's avatar
fxy060608 已提交
828

W
wanganxp 已提交
829 830 831 832
类是对象化的概念,有属性、方法、构造函数。
- 属性:是一个简单的值,可以是字符串、数字、布尔或另一个class。可以用 `对象.属性名` 的访问,也可以通过 `对象.属性名=xxx` 的方式赋值。
- 方法:是一段代码的集合,有入参、有返回值(均可选)。可以用 `对象.方法名(参数)` 的方式访问。
- 构造函数:用于初始化实例。详[见下](#constructor)
fxy060608's avatar
fxy060608 已提交
833

W
wanganxp 已提交
834
下面的示例中,定义了一个 Person 的 class,它有一个属性 name,有一个构造函数 constructor(名称不可改),还有一个方法 getNameLength。
fxy060608's avatar
fxy060608 已提交
835 836

```ts
W
wanganxp 已提交
837 838 839 840 841 842 843 844 845 846 847
// 定义Person Class
class Person {
	name:string = ""; // 属性name
	constructor(newname:string) { // 构造函数,参数newname
		console.log("开始实例化"); 
		this.name = newname;
	}
	getNameLength():number{ // 方法getNameLength
		return this.name.length
	}
}
fxy060608's avatar
fxy060608 已提交
848 849
```

W
wanganxp 已提交
850
定义了class后,需要实例化(通过new关键字)。定义一个实例后,即可使用该实例对象的属性和方法。
fxy060608's avatar
fxy060608 已提交
851

W
wanganxp 已提交
852
一个class可以被多次实例化为不同的实例,互不影响。
fxy060608's avatar
fxy060608 已提交
853

W
wanganxp 已提交
854 855 856 857 858
```ts
//实例化上面定义的class并调用其属性方法
let p = new Person("tom"); // 使用 new 关键字实例化对象时,会自动触发构造函数
console.log(p.name); // 访问p这个对象的属性name,返回值tom
console.log(p.getNameLength()); // 调用p这个对象的方法getNameLength,返回值3
fxy060608's avatar
fxy060608 已提交
859

W
wanganxp 已提交
860 861 862
let p2 = new Person("jerry"); // 使用 new 关键字再实例化一个新对象
console.log(p2.name); //jerry
console.log(p2.getNameLength()); //5
fxy060608's avatar
fxy060608 已提交
863 864 865 866


```

D
DCloud_LXH 已提交
867
### 构造函数(constructor)@uts-constructor
fxy060608's avatar
fxy060608 已提交
868

W
wanganxp 已提交
869
构造函数 constructor ,在创建新对象时(new的时候)会自动执行,用于初始化对象属性。
fxy060608's avatar
fxy060608 已提交
870 871 872 873

-   语法:

```ts
W
wanganxp 已提交
874
constructor([arguments]) { ... }
fxy060608's avatar
fxy060608 已提交
875 876 877 878
```

-   描述:

W
wanganxp 已提交
879 880 881
你可以不写构造函数。如果没有显式指定构造函数,运行环境会自动添加默认的 constructor 方法。

在一个类中只能有一个名为 “constructor” 的特殊方法。一个类中出现多次构造函数 (constructor)方法将会抛出一个 SyntaxError 错误。
fxy060608's avatar
fxy060608 已提交
882 883 884 885

-   示例:

```ts
W
wanganxp 已提交
886 887 888 889 890 891
class Person {
	name:string = "";
	constructor(newname:string) {
		this.name = newname;
	}
}
fxy060608's avatar
fxy060608 已提交
892

W
wanganxp 已提交
893 894
let person = new Person("tom"); // 使用 new 关键字创建对象时,会自动触发构造函数
console.log(person.name); // tom
fxy060608's avatar
fxy060608 已提交
895 896
```

W
wanganxp 已提交
897
在一个构造函数中可以使用 super 关键字来调用一个父类的构造函数。这涉及继承的概念。如不了解继承可[见下](#extends)
fxy060608's avatar
fxy060608 已提交
898 899
```ts
class Polygon {
W
wanganxp 已提交
900 901
    constructor() {
        this.name = "Polygon";
fxy060608's avatar
fxy060608 已提交
902 903 904 905
    }
}

class Square extends Polygon {
W
wanganxp 已提交
906 907
    constructor() {
        super();
fxy060608's avatar
fxy060608 已提交
908 909 910 911
    }
}
```

fxy060608's avatar
fxy060608 已提交
912
### 实例属性
fxy060608's avatar
fxy060608 已提交
913

W
wanganxp 已提交
914
class 有实例属性和静态属性。uts 中实例属性存在于类的每一个实例中。
fxy060608's avatar
fxy060608 已提交
915

fxy060608's avatar
fxy060608 已提交
916
#### 声明实例属性
fxy060608's avatar
fxy060608 已提交
917 918 919 920

uts 可以在类中声明属性,默认可读,可写。

```ts
W
wanganxp 已提交
921 922 923 924 925 926
class Person {
	name:string = ""; // 声明实例属性name
	city:string = "beijing" // 声明实例属性city
	constructor(newname:string) {
		this.name = newname; // 在构造函数中对name重新赋值
	}
fxy060608's avatar
fxy060608 已提交
927 928
}

W
wanganxp 已提交
929 930 931 932 933 934
let person1 = new Person("tom"); // 使用 new 关键字创建对象时,会自动触发构造函数
console.log(person1.name); //tom
console.log(person1.city); //beijing
let person2 = new Person("jerry"); // 使用 new 关键字创建对象时,会自动触发构造函数
console.log(person2.name); //jerry
console.log(person2.city); //beijing
fxy060608's avatar
fxy060608 已提交
935 936
```

fxy060608's avatar
fxy060608 已提交
937
#### Getter 与 Setter
fxy060608's avatar
fxy060608 已提交
938

W
wanganxp 已提交
939 940 941
uts 支持通过 getters/setters 来截取对对象属性的访问。它可以理解为属性的读取/写入的拦截器。

下面的例子中,针对 person对象提供了name的get和set的拦截,paascode不正确时无法修改name的值。
fxy060608's avatar
fxy060608 已提交
942 943 944

```ts
const passcode = "secret passcode";
W
wanganxp 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958
class Person {
	private _name: string = ""; // private是私有的,外部不能访问
	get name(): string { // 读取name会触发此拦截器
		console.log("start to get person.name");
		return this._name;
	}
	set name(newName: string) { // 给name赋值会触发此拦截器
		console.log("start to set person.name");
		if (passcode === "secret passcode") { // 校验是否有权修改name的值,这里的条件可以修改以方便测试
			this._name = newName;
		} else {
			console.log("Error: set person.name fail");
		}
	}
fxy060608's avatar
fxy060608 已提交
959
}
W
wanganxp 已提交
960 961 962
let p = new Person()
p.name = "tom" // 会打印"start to set person.name"
console.log(p.name); // 先打印"start to get person.name",然后打印"tom"
fxy060608's avatar
fxy060608 已提交
963 964
```

fxy060608's avatar
fxy060608 已提交
965
#### readonly
fxy060608's avatar
fxy060608 已提交
966

W
wanganxp 已提交
967
uts 可以使用 readonly 关键字将属性设置为只读的。只读属性必须在声明时或构造函数里被初始化。
fxy060608's avatar
fxy060608 已提交
968 969

```ts
W
wanganxp 已提交
970 971 972 973 974 975
class Person {
	readonly name: string;
	readonly age: number = 0;
	constructor (theName: string) {
		this.name = theName;
	}
fxy060608's avatar
fxy060608 已提交
976
}
W
wanganxp 已提交
977 978 979 980
let p = new Person("tom");
console.log(p.name);
p.name = "jerry"; // 错误! name 是只读的
p.age = 1 // 错误! age 是只读的
fxy060608's avatar
fxy060608 已提交
981 982
```

W
wanganxp 已提交
983 984 985
但 readonly 更多是一种开发环境的语法校验。在运行时,该值往往可以改变。

### 静态属性(static)
fxy060608's avatar
fxy060608 已提交
986 987 988 989

使用关键字 static 来将一个属性声明为静态属性。静态属性不会在实例中被调用,而只会被类本身调用。

```ts
W
wanganxp 已提交
990 991 992 993 994
class Person {
	static age:number = 10; // age是静态属性。不能在实例p中访问,但可以通过类Person访问
	getAge():number{
		return Person.age
	}
fxy060608's avatar
fxy060608 已提交
995
}
W
wanganxp 已提交
996 997 998
console.log(Person.age); //10
let p = new Person(); //新建一个实例
console.log(p.getAge()); //10
fxy060608's avatar
fxy060608 已提交
999 1000
```

fxy060608's avatar
fxy060608 已提交
1001
### 实例方法
fxy060608's avatar
fxy060608 已提交
1002 1003 1004

uts 中实例方法存在于类的每一个实例中。

fxy060608's avatar
fxy060608 已提交
1005
#### 声明实例方法
fxy060608's avatar
fxy060608 已提交
1006 1007 1008

uts 可以在类中声明实例方法。

W
wanganxp 已提交
1009 1010
下面定义一个通过高度乘以宽度计算面积的类。

fxy060608's avatar
fxy060608 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
```ts
class Rectangle {
    private height:number;
    private width:number;
    constructor(height: number, width: number) {
        this.height = height;
        this.width = width;
    }
    calcArea(): number {
        return this.height * this.width;
    }
}
```

使用一个实例方法,以类实例调用它即可:

```ts
const square = new Rectangle(10, 10);
W
wanganxp 已提交
1029
square.calcArea(); // 100
fxy060608's avatar
fxy060608 已提交
1030 1031
```

W
wanganxp 已提交
1032
### 静态方法(static)
fxy060608's avatar
fxy060608 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041

使用关键字 static 来将一个方法声明为静态方法。静态方法不会在实例中被调用,而只会被类本身调用。它们经常是工具函数,比如用来创建或者复制对象。

```ts
class ClassWithStaticMethod {
    static staticMethod(): string {
        return "static method has been called.";
    }
}
W
wanganxp 已提交
1042 1043 1044 1045 1046
ClassWithStaticMethod.staticMethod(); // 不实例化,直接调用class的方法
```

### 继承(extends)@extends

W
wanganxp 已提交
1047 1048 1049
uts 允许使用继承来扩展现有的类。扩展的子类继承了父类的属性方法,但又可以添加自己独有的属性方法,以及复写父类定义的属性方法。

被继承的类称为父类(也称为超类、基类),新扩展的类称为子类(也称为派生类)。
W
wanganxp 已提交
1050

W
wanganxp 已提交
1051
比如定义了Person类存储人的基本信息,还可以定义一个Developer子类继承自Person类,在子类里追加Developer的独有信息。
W
wanganxp 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060

-   语法:

```ts
class ChildClass extends ParentClass { ... }
```

-   描述:

W
wanganxp 已提交
1061
extends 关键字用来创建一个类的子类。
W
wanganxp 已提交
1062 1063 1064 1065 1066 1067

-   示例:

```ts
// 定义父类
class Person {
W
wanganxp 已提交
1068 1069 1070
	name:string = "";
	constructor(newname:string) {
		this.name = newname;
W
wanganxp 已提交
1071
	}
W
wanganxp 已提交
1072 1073
}
// 定义子类
W
wanganxp 已提交
1074 1075 1076 1077
class Developer extends Person{
	likeLanguage:string = "ts"
}

W
wanganxp 已提交
1078
let d = new Developer("tom"); // 实例化。由于子类没有声明和复写自己的构造函数,所以默认继承了父类的构造函数
W
wanganxp 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087
console.log(d.name); // tom
console.log(d.likeLanguage); // ts
```

- 如果要控制父类中某些属性方法不被子类继承,可使用可见性修饰符(private、protected等),具体[见下](#modifier)
- 多重继承:子类还可以被孙类继承

#### 覆盖方法(override)

W
wanganxp 已提交
1088 1089 1090
覆盖,也称为复写、重写。在继承中,用于在子类中改写父类定义的方法或属性。

uts 对于可覆盖的成员以及覆盖后的成员需要显式修饰符override。
W
wanganxp 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

```ts
class Polygon {
    name(): string {
        return "Polygon";
    }
}

class Square extends Polygon {
    override name(): string {
        return "Square";
    }
}
```

Square.name 函数上必须加上 override 修饰符。如果没写,编译器会报错。

#### 覆盖属性

W
wanganxp 已提交
1110
属性与方法的覆盖机制相同。父类中已声明的同名属性,在子类中重新声明必须以 override 开头,并且它们必须具有兼容的类型(都是字符串、或数字、布尔值等)。
W
wanganxp 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

```ts
class Shape {
     vertexCount: Int = 0
}

class Rectangle extends Shape {
    override  vertexCount = 4
}
```

W
wanganxp 已提交
1122
#### 调用父类实现
W
wanganxp 已提交
1123

W
wanganxp 已提交
1124
子类中的代码可以使用 super 关键字调用其父类的方法。不能跨级调用父类的父类(爷爷类)的方法。
W
wanganxp 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

```ts
class Rectangle {
    draw() {}
}
class FilledRectangle extends Rectangle {
    override draw() {
        super.draw();
    }
}

fxy060608's avatar
fxy060608 已提交
1136 1137
```

W
wanganxp 已提交
1138 1139

### 可见性修饰符@modifier
fxy060608's avatar
fxy060608 已提交
1140 1141 1142 1143 1144

类的方法与属性都可以有可见性修饰符。

在 uts 中有三个可见性修饰符:private、 protected、 和 public。 默认可见性是 public。

fxy060608's avatar
fxy060608 已提交
1145
#### public
fxy060608's avatar
fxy060608 已提交
1146 1147 1148

在 uts 中可以自由的访问程序里定义的 public 成员,这也是 uts 的默认行为。

fxy060608's avatar
fxy060608 已提交
1149
#### private
fxy060608's avatar
fxy060608 已提交
1150 1151 1152 1153

当成员被标记成 private 时,它就不能在声明它的类的外部访问。比如:

```ts
W
wanganxp 已提交
1154
class Person {
fxy060608's avatar
fxy060608 已提交
1155 1156 1157
    private name: string = "Cat";
}

W
wanganxp 已提交
1158
new Person().name; // 错误: 'name' 是私有的.
fxy060608's avatar
fxy060608 已提交
1159 1160
```

fxy060608's avatar
fxy060608 已提交
1161
#### protected
fxy060608's avatar
fxy060608 已提交
1162

W
wanganxp 已提交
1163
protected 修饰符与 private 修饰符的行为很相似,但有一点不同,protected 成员在继承的派生类中仍然可以访问。比如:
fxy060608's avatar
fxy060608 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

```ts
class Person {
    protected name: string;
    constructor(name: string) {
        this.name = name;
    }
}

class Employee extends Person {
    private department: string;

    constructor(name: string, department: string) {
        super(name);
        this.department = department;
    }

    public getElevatorPitch(): string {
        return `Hello, my name is ${this.name} and I work in ${this.department}.`;
    }
}
const howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // 错误
```

注意,我们不能在 Person 类外使用 name,但是我们仍然可以通过 Employee 类的实例方法访问,因为 Employee 是由 Person 派生而来的。

fxy060608's avatar
fxy060608 已提交
1192
## 模块
fxy060608's avatar
fxy060608 已提交
1193 1194 1195

uts 支持将程序拆分为可按需导入的单独模块,模块中可以导入和导出各种类型的变量,如函数,字符串,数字,布尔值,类等。

fxy060608's avatar
fxy060608 已提交
1196
### 导出
fxy060608's avatar
fxy060608 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

export 语句可以将一个文件中的函数,类等导出。比如:

```ts
export const name: string = "square";
export function draw() {}
export default class Canvas {} // default 关键词支持默认导出
```

- 导出的函数声明与类声明必须要有名称。
- export 命令可以出现在模块的任何位置,但必需处于模块顶层。
- 在一个文件中,export、import 可以有多个,export default 仅有一个。
- 通过 export 方式导出,在导入时要加{ },export default 则不需要。

fxy060608's avatar
fxy060608 已提交
1211
### 导入
fxy060608's avatar
fxy060608 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

import 语句可以将另一个文件中的函数,类等导入到当前文件。比如:

```ts
import { name as name1, draw } from "./canvas.uts" // 支持 as 语法做别名导入
import * as Utils from "./utils.uts" // Test 包含所有 export 的导出
import Canvas from "./canvas.uts" // 对应 export default 的导出
```

示例

```ts
/*-----export [test.js]-----*/
export const name = 'test'
export function test(){
    console.log('test')
}
export default class Test{
    test(){
        console.log('Test.test')
    }
}
```

```ts
import { name } from './test.uts'
import * as testModule from './test.uts'
import Test from './test.uts'
console.log(name)
testModule.test()
const test = new Test()
test.test()
```

W
wanganxp 已提交
1246
## 内置对象和API
fxy060608's avatar
fxy060608 已提交
1247

W
wanganxp 已提交
1248 1249
uts 有一批内置对象。不管将 uts 编译为 js/kotlin/swfit,这些内置对象都可以跨平台使用。

fxy060608's avatar
fxy060608 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
### console

#### debug
在控制台打印 debug 日志
```ts
console.debug(msg1, msg2, msg3)
```
#### error
在控制台打印 error 日志
```ts
console.error(msg1, msg2, msg3)
```
#### info
在控制台打印 info 日志
```ts
console.info(msg1, msg2, msg3)
```
#### log
在控制台打印 log 日志
```ts
console.log(msg1, msg2, msg3)
```
#### warn
在控制台打印 warn 日志
```ts
console.warn(msg1, msg2, msg3)
```

fxy060608's avatar
fxy060608 已提交
1278
### Array
fxy060608's avatar
fxy060608 已提交
1279 1280 1281

Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。

fxy060608's avatar
fxy060608 已提交
1282
#### 实例属性
fxy060608's avatar
fxy060608 已提交
1283

fxy060608's avatar
fxy060608 已提交
1284
#### length
fxy060608's avatar
fxy060608 已提交
1285 1286 1287

数组中的元素个数

fxy060608's avatar
fxy060608 已提交
1288 1289 1290 1291 1292 1293
```ts
const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];
console.log(clothing.length);
// expected output: 4
```

Y
yurj26 已提交
1294 1295 1296 1297 1298 1299 1300
注意事项:

- 在不同平台上,数组的长度限制不同,超出限制会导致相应的错误或异常
	* 编译至 JavaScript 平台时,最大长度为 2^32 - 1,超出限制会报错:`Invalid array length`
	* 编译至 Kotlin 平台时,最大长度受系统内存的限制,超出限制报错:`java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM`
  * 编译至 Swift 平台时,最大长度也受系统内存的限制,目前超出限制没有返回信息。

fxy060608's avatar
fxy060608 已提交
1301
#### 实例方法
fxy060608's avatar
fxy060608 已提交
1302

fxy060608's avatar
fxy060608 已提交
1303
#### concat
fxy060608's avatar
fxy060608 已提交
1304

fxy060608's avatar
fxy060608 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313
concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

```ts
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
```
fxy060608's avatar
fxy060608 已提交
1314

fxy060608's avatar
fxy060608 已提交
1315
#### copyWithin
fxy060608's avatar
fxy060608 已提交
1316

fxy060608's avatar
fxy060608 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
copyWithin() 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。

```ts
const array1 = ['a', 'b', 'c', 'd', 'e'];
// copy to index 0 the element at index 3
console.log(array1.copyWithin(0, 3, 4));
// expected output: Array ["d", "b", "c", "d", "e"]
// copy to index 1 all elements from index 3 to the end
console.log(array1.copyWithin(1, 3));
// expected output: Array ["d", "d", "e", "d", "e"]
```
fxy060608's avatar
fxy060608 已提交
1328

fxy060608's avatar
fxy060608 已提交
1329
#### every
fxy060608's avatar
fxy060608 已提交
1330

fxy060608's avatar
fxy060608 已提交
1331 1332 1333 1334 1335 1336 1337 1338
every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

```ts
const isBelowThreshold = (currentValue:number):boolean => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
```
fxy060608's avatar
fxy060608 已提交
1339

fxy060608's avatar
fxy060608 已提交
1340
#### fill
fxy060608's avatar
fxy060608 已提交
1341

fxy060608's avatar
fxy060608 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。

```ts
const array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]

// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]
fxy060608's avatar
fxy060608 已提交
1354

fxy060608's avatar
fxy060608 已提交
1355 1356 1357 1358
console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]

```
fxy060608's avatar
fxy060608 已提交
1359
#### filter
fxy060608's avatar
fxy060608 已提交
1360

fxy060608's avatar
fxy060608 已提交
1361
filter() 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素。
fxy060608's avatar
fxy060608 已提交
1362

fxy060608's avatar
fxy060608 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371
```ts
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter((word:string):boolean => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

```
fxy060608's avatar
fxy060608 已提交
1372
#### find
fxy060608's avatar
fxy060608 已提交
1373

fxy060608's avatar
fxy060608 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
find() 方法返回数组中满足提供的测试函数的第一个元素的值。

```ts
const array1 = [5, 12, 8, 130, 44];

const found = array1.find((element:number):boolean => element > 10);

console.log(found);
// expected output: 12

```
fxy060608's avatar
fxy060608 已提交
1385

fxy060608's avatar
fxy060608 已提交
1386
#### findIndex
fxy060608's avatar
fxy060608 已提交
1387

fxy060608's avatar
fxy060608 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

```ts
const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element:number):boolean => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

```
fxy060608's avatar
fxy060608 已提交
1399

fxy060608's avatar
fxy060608 已提交
1400
#### flat
fxy060608's avatar
fxy060608 已提交
1401

fxy060608's avatar
fxy060608 已提交
1402 1403 1404 1405
flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。

```ts
const arr1 = [0, 1, 2, [3, 4]];
fxy060608's avatar
fxy060608 已提交
1406

fxy060608's avatar
fxy060608 已提交
1407 1408
console.log(arr1.flat());
// expected output: [0, 1, 2, 3, 4]
fxy060608's avatar
fxy060608 已提交
1409

fxy060608's avatar
fxy060608 已提交
1410 1411 1412 1413 1414
const arr2 = [0, 1, 2, [[[3, 4]]]];

console.log(arr2.flat(2));
// expected output: [0, 1, 2, [3, 4]]
```
fxy060608's avatar
fxy060608 已提交
1415

fxy060608's avatar
fxy060608 已提交
1416
#### forEach
fxy060608's avatar
fxy060608 已提交
1417

fxy060608's avatar
fxy060608 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426
forEach() 方法对数组的每个元素执行一次给定的函数。

```ts
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
```
fxy060608's avatar
fxy060608 已提交
1427

fxy060608's avatar
fxy060608 已提交
1428
#### includes
fxy060608's avatar
fxy060608 已提交
1429

fxy060608's avatar
fxy060608 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false。

```ts
const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false

```
fxy060608's avatar
fxy060608 已提交
1447

fxy060608's avatar
fxy060608 已提交
1448
#### indexOf
fxy060608's avatar
fxy060608 已提交
1449

fxy060608's avatar
fxy060608 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
indexOf() 方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。

```ts
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
// expected output: -1

```
fxy060608's avatar
fxy060608 已提交
1466

fxy060608's avatar
fxy060608 已提交
1467
#### join
fxy060608's avatar
fxy060608 已提交
1468

fxy060608's avatar
fxy060608 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
join() 方法将一个数组的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。

```ts
const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

```
fxy060608's avatar
fxy060608 已提交
1484

fxy060608's avatar
fxy060608 已提交
1485
#### lastIndexOf
fxy060608's avatar
fxy060608 已提交
1486

fxy060608's avatar
fxy060608 已提交
1487 1488 1489 1490 1491 1492 1493
lastIndexOf() 方法返回指定元素在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。

```ts
const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];

console.log(animals.lastIndexOf('Dodo'));
// expected output: 3
fxy060608's avatar
fxy060608 已提交
1494

fxy060608's avatar
fxy060608 已提交
1495 1496 1497 1498
console.log(animals.lastIndexOf('Tiger'));
// expected output: 1

```
fxy060608's avatar
fxy060608 已提交
1499
#### map
fxy060608's avatar
fxy060608 已提交
1500

fxy060608's avatar
fxy060608 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
map() 方法创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。

```ts
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map((x:number):number => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

```
fxy060608's avatar
fxy060608 已提交
1513

fxy060608's avatar
fxy060608 已提交
1514
#### pop
fxy060608's avatar
fxy060608 已提交
1515

fxy060608's avatar
fxy060608 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
pop() 方法从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度。

```ts
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];

console.log(plants.pop());
// expected output: "tomato"

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]

plants.pop();

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]

```
fxy060608's avatar
fxy060608 已提交
1533

fxy060608's avatar
fxy060608 已提交
1534
#### push
fxy060608's avatar
fxy060608 已提交
1535

fxy060608's avatar
fxy060608 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。

```ts
const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

```
fxy060608's avatar
fxy060608 已提交
1552

fxy060608's avatar
fxy060608 已提交
1553
#### reduce
fxy060608's avatar
fxy060608 已提交
1554

fxy060608's avatar
fxy060608 已提交
1555
reduce() 方法对数组中的每个元素按序执行一个由您提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。
fxy060608's avatar
fxy060608 已提交
1556

fxy060608's avatar
fxy060608 已提交
1557
第一次执行回调函数时,不存在“上一次的计算结果”。如果需要回调函数从数组索引为 0 的元素开始执行,则需要传递初始值。否则,数组索引为 0 的元素将被作为初始值 initialValue,迭代器将从第二个元素开始执行(索引为 1 而不是 0)。
fxy060608's avatar
fxy060608 已提交
1558

fxy060608's avatar
fxy060608 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
```ts
const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue:number, currentValue:number):number => previousValue + currentValue,
  initialValue
);

console.log(sumWithInitial);
// expected output: 10

```
fxy060608's avatar
fxy060608 已提交
1573

fxy060608's avatar
fxy060608 已提交
1574
#### shift
fxy060608's avatar
fxy060608 已提交
1575

fxy060608's avatar
fxy060608 已提交
1576
shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
fxy060608's avatar
fxy060608 已提交
1577

Y
yurj26 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
```ts
const array1 = [1, 2, 3];

const firstElement = array1.shift();

console.log(array1);
// expected output: Array [2, 3]

console.log(firstElement);
// expected output: 1

```

fxy060608's avatar
fxy060608 已提交
1591
#### slice
fxy060608's avatar
fxy060608 已提交
1592

fxy060608's avatar
fxy060608 已提交
1593
slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。
fxy060608's avatar
fxy060608 已提交
1594

Y
yurj26 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
```ts
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]

console.log(animals.slice());
// expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
```

fxy060608's avatar
fxy060608 已提交
1617
#### some
fxy060608's avatar
fxy060608 已提交
1618

fxy060608's avatar
fxy060608 已提交
1619
some() 方法测试数组中是不是至少有 1 个元素通过了被提供的函数测试。它返回的是一个 Boolean 类型的值。
fxy060608's avatar
fxy060608 已提交
1620

Y
yurj26 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
```ts
const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element:number):boolean=> element % 2 == 0;

console.log(array.some(even));
// expected output: true
```

Y
yurj26 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
#### sort

sort() 方法对数组的元素进行排序,并返回数组。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
|√|x|x|

fxy060608's avatar
fxy060608 已提交
1641
#### splice
fxy060608's avatar
fxy060608 已提交
1642

fxy060608's avatar
fxy060608 已提交
1643
splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
fxy060608's avatar
fxy060608 已提交
1644

Y
yurj26 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
```ts
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
```

fxy060608's avatar
fxy060608 已提交
1658
#### unshift
fxy060608's avatar
fxy060608 已提交
1659

fxy060608's avatar
fxy060608 已提交
1660
unshift() 方法将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)。
fxy060608's avatar
fxy060608 已提交
1661

Y
yurj26 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
```ts
const array1 = [1, 2, 3];

console.log(array1.unshift(4, 5));
// expected output: 5

console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]
```

fxy060608's avatar
fxy060608 已提交
1672
#### 常见操作
fxy060608's avatar
fxy060608 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745

- 创建数组
```ts
const fruits = ['Apple', 'Banana']
console.log(fruits.length)
```
- 通过索引访问数组元素
```ts
const first = fruits[0]
// Apple
const last = fruits[fruits.length - 1]
// Banana
```
- 遍历数组
```ts
fruits.forEach(function(item, index, array) {
  console.log(item, index)
})
// Apple 0
// Banana 1
```
- 添加元素到数组的末尾
```ts
const newLength = fruits.push('Orange')
// ["Apple", "Banana", "Orange"]
```
- 删除数组末尾的元素
```ts
const last = fruits.pop() // remove Orange (from the end)
// ["Apple", "Banana"]
```
- 删除数组头部元素
```ts
const first = fruits.shift() // remove Apple from the front
// ["Banana"]
```
- 添加元素到数组的头部
```ts
const newLength = fruits.unshift('Strawberry') // add to the front
// ["Strawberry", "Banana"]
```
- 找出某个元素在数组中的索引
```ts
fruits.push('Mango')
// ["Strawberry", "Banana", "Mango"]
const pos = fruits.indexOf('Banana')
// 1
```
- 通过索引删除某个元素
```ts
const removedItem = fruits.splice(pos, 1) // this is how to remove an item
// ["Strawberry", "Mango"]
```
- 从一个索引位置删除多个元素
```ts
const vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
console.log(vegetables)
// ["Cabbage", "Turnip", "Radish", "Carrot"]
const pos = 1
const n = 2
const removedItems = vegetables.splice(pos, n)
// this is how to remove items, n defines the number of items to be removed,
// starting at the index position specified by pos and progressing toward the end of array.
console.log(vegetables)
// ["Cabbage", "Carrot"] (the original array is changed)
console.log(removedItems)
// ["Turnip", "Radish"]
```
- 复制一个数组
```ts
const shallowCopy = fruits.slice() // this is how to make a copy
// ["Strawberry", "Mango"]
```
fxy060608's avatar
fxy060608 已提交
1746
#### 访问数组元素
fxy060608's avatar
fxy060608 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757

数组的索引是从 0 开始的,第一个元素的索引为 0,最后一个元素的索引等于该数组的 长度 减 1。

如果指定的索引是一个无效值,将会抛出 IndexOutOfBoundsException 异常

下面的写法是错误的,运行时会抛出 SyntaxError 异常,而原因则是使用了非法的属性名:

```ts
console.log(arr.0) // a syntax error
```

fxy060608's avatar
fxy060608 已提交
1758

fxy060608's avatar
fxy060608 已提交
1759
### Date
fxy060608's avatar
fxy060608 已提交
1760 1761 1762

创建一个 Date 实例,该实例呈现时间中的某个时刻。Date 对象则基于 Unix Time Stamp,即自 1970 年 1 月 1 日(UTC)起经过的毫秒数。

fxy060608's avatar
fxy060608 已提交
1763
#### 语法
fxy060608's avatar
fxy060608 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774

```ts
new Date();
new Date(value);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
```

- 如果没有输入任何参数,则 Date 的构造器会依据系统设置的当前时间来创建一个 Date 对象。
- 如果提供了至少两个参数,其余的参数均会默认设置为 1(如果没有指定 day 参数)或者 0(如果没有指定 day 以外的参数)。
- uts 的时间由世界标准时间(UTC)1970 年 1 月 1 日开始,用毫秒计时,一天由 86,400,000 毫秒组成。Date 对象的范围是 -100,000,000 天至 100,000,000 天(等效的毫秒值)。
- 
fxy060608's avatar
fxy060608 已提交
1775 1776
#### 静态方法
#### now
fxy060608's avatar
fxy060608 已提交
1777 1778 1779

表示自 UNIX 纪元开始(1970 年 1 月 1 日 00:00:00 (UTC))到当前时间的毫秒数。

fxy060608's avatar
fxy060608 已提交
1780 1781
**平台差异说明**

1782
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1783
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1784
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1785

fxy060608's avatar
fxy060608 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
```ts
// this example takes 2 seconds to run
const start = Date.now()
console.log('starting timer...')
// expected output: starting timer...
setTimeout(() => {
  const millis = Date.now() - start
  console.log(`seconds elapsed = ${Math.floor(millis / 1000)}`)
  // expected output: seconds elapsed = 2
}, 2000)
```

fxy060608's avatar
fxy060608 已提交
1798
#### 实例方法
fxy060608's avatar
fxy060608 已提交
1799

fxy060608's avatar
fxy060608 已提交
1800
#### getDate
fxy060608's avatar
fxy060608 已提交
1801 1802 1803

根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从 1--31)。

fxy060608's avatar
fxy060608 已提交
1804 1805
**平台差异说明**

1806
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1807
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1808
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1809

fxy060608's avatar
fxy060608 已提交
1810
#### getDay
fxy060608's avatar
fxy060608 已提交
1811 1812 1813

根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。对于某个月中的第几天

fxy060608's avatar
fxy060608 已提交
1814 1815
**平台差异说明**

1816
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1817
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1818
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1819

fxy060608's avatar
fxy060608 已提交
1820
#### getFullYear
fxy060608's avatar
fxy060608 已提交
1821 1822 1823

根据本地时间返回指定日期的年份。

fxy060608's avatar
fxy060608 已提交
1824
#### getHours
fxy060608's avatar
fxy060608 已提交
1825 1826 1827

根据本地时间,返回一个指定的日期对象的小时。

fxy060608's avatar
fxy060608 已提交
1828 1829
**平台差异说明**

1830
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1831
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1832
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1833

fxy060608's avatar
fxy060608 已提交
1834
#### getMilliseconds
fxy060608's avatar
fxy060608 已提交
1835 1836 1837

根据本地时间,返回一个指定的日期对象的毫秒数。

fxy060608's avatar
fxy060608 已提交
1838 1839
**平台差异说明**

1840
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1841
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1842
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1843

fxy060608's avatar
fxy060608 已提交
1844
#### getMinutes
fxy060608's avatar
fxy060608 已提交
1845 1846 1847

根据本地时间,返回一个指定的日期对象的分钟数。

fxy060608's avatar
fxy060608 已提交
1848 1849
**平台差异说明**

1850
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1851
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1852
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1853

fxy060608's avatar
fxy060608 已提交
1854
#### getMonth
fxy060608's avatar
fxy060608 已提交
1855 1856 1857

指定的日期对象的月份,为基于 0 的值(0 表示一年中的第一月)。

fxy060608's avatar
fxy060608 已提交
1858 1859
**平台差异说明**

1860
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1861
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1862
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1863

fxy060608's avatar
fxy060608 已提交
1864
#### getSeconds
fxy060608's avatar
fxy060608 已提交
1865 1866 1867

根据本地时间,返回一个指定的日期对象的秒数。

fxy060608's avatar
fxy060608 已提交
1868 1869
**平台差异说明**

1870
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1871
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1872
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1873

fxy060608's avatar
fxy060608 已提交
1874
#### getTime
fxy060608's avatar
fxy060608 已提交
1875 1876 1877

返回一个时间的格林威治时间数值。

fxy060608's avatar
fxy060608 已提交
1878 1879
**平台差异说明**

1880
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1881
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1882
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1883

fxy060608's avatar
fxy060608 已提交
1884
#### setDate
fxy060608's avatar
fxy060608 已提交
1885 1886 1887

根据本地时间来指定一个日期对象的天数。

fxy060608's avatar
fxy060608 已提交
1888 1889
**平台差异说明**

1890
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1891
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1892
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1893

fxy060608's avatar
fxy060608 已提交
1894
#### setFullYear
fxy060608's avatar
fxy060608 已提交
1895 1896 1897

根据本地时间为一个日期对象设置年份。

fxy060608's avatar
fxy060608 已提交
1898 1899
**平台差异说明**

1900
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1901
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1902
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1903

fxy060608's avatar
fxy060608 已提交
1904
#### setHours
fxy060608's avatar
fxy060608 已提交
1905 1906 1907

根据本地时间为一个日期对象设置小时数,返回从 1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。

fxy060608's avatar
fxy060608 已提交
1908
#### setMilliseconds
fxy060608's avatar
fxy060608 已提交
1909 1910 1911

根据本地时间设置一个日期对象的豪秒数。

fxy060608's avatar
fxy060608 已提交
1912
#### setMinutes
fxy060608's avatar
fxy060608 已提交
1913 1914 1915

根据本地时间为一个日期对象设置分钟数。

fxy060608's avatar
fxy060608 已提交
1916
#### setMonth
fxy060608's avatar
fxy060608 已提交
1917 1918 1919

根据本地时间为一个日期对象设置月份。

fxy060608's avatar
fxy060608 已提交
1920
#### setSeconds
fxy060608's avatar
fxy060608 已提交
1921 1922 1923

根据本地时间设置一个日期对象的秒数。

fxy060608's avatar
fxy060608 已提交
1924
#### setTime
fxy060608's avatar
fxy060608 已提交
1925 1926 1927

以一个表示从 1970-1-1 00:00:00 UTC 计时的毫秒数为来为 Date 对象设置时间。

fxy060608's avatar
fxy060608 已提交
1928 1929
**平台差异说明**

1930
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
1931
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
1932
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
1933

fxy060608's avatar
fxy060608 已提交
1934
### Error
fxy060608's avatar
fxy060608 已提交
1935 1936 1937

当运行时错误产生时,Error 对象会被抛出。Error 对象也可用于用户自定义的异常的基础对象。

fxy060608's avatar
fxy060608 已提交
1938
#### 实例属性
fxy060608's avatar
fxy060608 已提交
1939

fxy060608's avatar
fxy060608 已提交
1940
#### message
fxy060608's avatar
fxy060608 已提交
1941 1942
错误消息。对于用户创建的 Error 对象,这是构造函数的第一个参数提供的字符串。

fxy060608's avatar
fxy060608 已提交
1943
#### 示例
fxy060608's avatar
fxy060608 已提交
1944 1945 1946 1947 1948

```ts
try {
  throw new Error('Whoops!')
} catch (e) {
fxy060608's avatar
fxy060608 已提交
1949
  console.error(e.message)
fxy060608's avatar
fxy060608 已提交
1950 1951 1952
}
```

fxy060608's avatar
fxy060608 已提交
1953
### JSON
fxy060608's avatar
fxy060608 已提交
1954

fxy060608's avatar
fxy060608 已提交
1955
#### 静态方法
fxy060608's avatar
fxy060608 已提交
1956

fxy060608's avatar
fxy060608 已提交
1957
#### parse
fxy060608's avatar
fxy060608 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971

JSON.parse() 方法用来解析 JSON 字符串,构造由字符串描述的 UTSJSONObject。

```ts
const json = `{"result":true, "count":42}`;
const obj = JSON.parse(json);

console.log(obj["count"]);
// expected output: 42

console.log(obj["result"]);
// expected output: true
```

fxy060608's avatar
fxy060608 已提交
1972 1973 1974 1975
**注意**

- JSON.parse 解析出来的对象,目前仅支持使用方括号[]访问

fxy060608's avatar
fxy060608 已提交
1976
#### stringify
fxy060608's avatar
fxy060608 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990

JSON.stringify() 方法将一个 uts 对象或值转换为 JSON 字符串

```ts
console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"

console.log(JSON.stringify([3, 'false', boolean]));
// expected output: "[3,"false",false]"

console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// expected output: ""2006-01-02T15:04:05.000Z""

```
fxy060608's avatar
fxy060608 已提交
1991
### Map
fxy060608's avatar
fxy060608 已提交
1992 1993 1994

Map 对象保存键值对。任何值(对象或者基本类型)都可以作为一个键或一个值。

fxy060608's avatar
fxy060608 已提交
1995
#### 实例属性
fxy060608's avatar
fxy060608 已提交
1996

fxy060608's avatar
fxy060608 已提交
1997
#### size
fxy060608's avatar
fxy060608 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

返回 Map 对象的成员数量。

```ts
const map1 = new Map<string,string>();
map1.set('a', 'alpha');
map1.set('b', 'beta');
map1.set('g', 'gamma');
console.log(map1.size);
// expected output: 3

```

fxy060608's avatar
fxy060608 已提交
2011
#### 实例方法
fxy060608's avatar
fxy060608 已提交
2012

fxy060608's avatar
fxy060608 已提交
2013
#### clear
fxy060608's avatar
fxy060608 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027

移除 Map 对象中的所有元素。

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'baz');
map1.set(1, 'foo');
console.log(map1.size);
// expected output: 2
map1.clear();
console.log(map1.size);
// expected output: 0
```

fxy060608's avatar
fxy060608 已提交
2028
##### delete
fxy060608's avatar
fxy060608 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041

用于移除 Map 对象中指定的元素。

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');
console.log(map1.delete('bar'));
// expected result: true
// (true indicates successful removal)
console.log(map1.has('bar'));
// expected result: false
```

fxy060608's avatar
fxy060608 已提交
2042
#### get
fxy060608's avatar
fxy060608 已提交
2043 2044 2045

返回某个 Map 对象中的一个指定元素。

Y
yurj26 已提交
2046 2047 2048 2049 2050 2051 2052 2053
```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');

console.log(map1.get('bar'));
// expected output: "foo"
```

fxy060608's avatar
fxy060608 已提交
2054
#### has
fxy060608's avatar
fxy060608 已提交
2055 2056 2057

返回一个布尔值,用来表明 Map 中是否存在指定元素。

Y
yurj26 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');

console.log(map1.has('bar'));
// expected output: true

console.log(map1.has('baz'));
// expected output: false
```

fxy060608's avatar
fxy060608 已提交
2069
#### set
fxy060608's avatar
fxy060608 已提交
2070 2071 2072

添加或更新一个指定了键(key)和值(value)的(新)键值对。

Y
yurj26 已提交
2073 2074 2075 2076 2077 2078 2079 2080
```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');

console.log(map1.get('bar'));
// expected output: "foo"

console.log(map1.get('baz'));
Y
yurj26 已提交
2081
// expected output: null
Y
yurj26 已提交
2082 2083
```

Y
yurj26 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
### Math

Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。

#### 实例属性

#### E

Math.E 属性表示自然对数的底数(或称为基数),e,约等于 2.718。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2098
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2099 2100

```ts
Y
yurj26 已提交
2101
function getNapier():number {
Y
yurj26 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
  return Math.E;
}
console.log(getNapier());
// expected output: 2.718281828459045
```

#### LN10

Math.LN10 属性表示 10 的自然对数,约为 2.302。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2116
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2117 2118

```ts
Y
yurj26 已提交
2119
function getNatLog10():number {
Y
yurj26 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
  return Math.LN10;
}
console.log(getNatLog10());
// expected output: 2.302585092994046
```

#### LN2

Math.LN2 属性表示 2 的自然对数,约为 0.693。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2134
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2135 2136

```ts
Y
yurj26 已提交
2137
function getNatLog2():number {
Y
yurj26 已提交
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
  return Math.LN2;
}
console.log(getNatLog2());
// expected output: 0.6931471805599453
```

#### LOG10E

Math.LOG10E 属性表示以 10 为底数,e 的对数,约为 0.434。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2152
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2153 2154

```ts
Y
yurj26 已提交
2155
function getLog10e():number {
Y
yurj26 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
  return Math.LOG10E;
}
console.log(getLog10e());
// expected output: 0.4342944819032518
```

#### LOG2E

Math.LOG2E 属性表示以 2 为底数,e 的对数,约为 1.442。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2170
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2171 2172

```ts
Y
yurj26 已提交
2173
function getLog2e():number {
Y
yurj26 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
  return Math.LOG2E;
}
console.log(getLog2e());
// expected output: 1.4426950408889634
```

#### PI

Math.PI 表示一个圆的周长与直径的比例,约为 3.14159。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2188
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205

```ts
function calculateCircumference (radius:number):number {
  return 2 * Math.PI * radius;
}
console.log(calculateCircumference(1));
// expected output: 6.283185307179586
```

#### SQRT1_2

Math.SQRT1_2 属性表示 1/2 的平方根,约为 0.707。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2206
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2207 2208

```ts
Y
yurj26 已提交
2209
function getRoot1_2():number {
Y
yurj26 已提交
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
  return Math.SQRT1_2;
}
console.log(getRoot1_2());
// expected output: 0.7071067811865476
```

#### SQRT2

Math.SQRT2 属性表示 2 的平方根,约为 1.414。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2224
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2225 2226

```ts
Y
yurj26 已提交
2227
function getRoot2():number {
Y
yurj26 已提交
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
  return Math.SQRT2;
}
console.log(getRoot2());
// expected output: 1.4142135623730951
```
#### 实例方法

#### abs

Math.abs(x) 函数返回一个数字的绝对值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2243
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267

```ts
function difference(a:number, b:number):number {
  return Math.abs(a - b);
}

console.log(difference(3, 5));
// expected output: 2

console.log(difference(5, 3));
// expected output: 2

console.log(difference(1.23456, 7.89012));
// expected output: 6.6555599999999995
```

#### acos

Math.acos() 返回一个数的反余弦值(单位为弧度)。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2268
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288

```ts
console.log(Math.acos(-1));
// expected output: 3.141592653589793

console.log(Math.acos(0));
// expected output: 1.5707963267948966

console.log(Math.acos(1));
// expected output: 0
```

#### acosh

Math.acosh() 函数返回一个数的反双曲余弦值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2289
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309

```ts
console.log(Math.acosh(1));
// expected output: 0

console.log(Math.acosh(2));
// expected output: 1.3169578969248166

console.log(Math.acosh(2.5));
// expected output: 1.566799236972411
```

#### asin

Math.asin() 方法返回一个数值的反正弦(单位为弧度)。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2310
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

```ts
console.log(Math.asin(-1));
// expected output: -1.5707963267948966 (-pi/2)

console.log(Math.asin(0));
// expected output: 0

console.log(Math.asin(0.5));
// expected output: 0.5235987755982989

console.log(Math.asin(1));
Y
yurj26 已提交
2323
// expected output: 1.5707963267948966
Y
yurj26 已提交
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
```

#### asinh

Math.asinh() 返回一个数值的反双曲正弦值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2334
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357

```ts
console.log(Math.asinh(1));
// expected output: 0.881373587019543

console.log(Math.asinh(0));
// expected output: 0

console.log(Math.asinh(-1));
// expected output: -0.881373587019543

console.log(Math.asinh(2));
// expected output: 1.4436354751788103
```

#### atan

Math.atan() 函数返回一个数值的反正切(以弧度为单位)。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2358
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375

```ts
console.log(Math.atan(1));
// expected output: 0.7853981633974483

console.log(Math.atan(0));
// expected output: 0
```

#### atan2

Math.atan2() 返回从原点 (0,0) 到 (x,y) 点的线段与 x 轴正方向之间的平面角度 (弧度值),也就是 Math.atan2(y,x)。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2376
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393

```ts
console.log(Math.atan2(90, 15));
// expected output: 1.4056476493802699

console.log(Math.atan2(15, 90));
// expected output: 0.16514867741462683
```

#### atanh

Math.atanh() 函数返回一个数值反双曲正切值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2394
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411

```ts
console.log(Math.atanh(0));
// expected output: 0

console.log(Math.atanh(0.5));
// expected output: 0.5493061443340548
```

#### cbrt

Math.cbrt() 函数返回任意数字的立方根。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2412
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2413 2414 2415 2416 2417

```ts
console.log(Math.cbrt(-1));
// expected output: -1

Y
yurj26 已提交
2418
console.log(Math.cbrt(0));
Y
yurj26 已提交
2419 2420
// expected output: 0

Y
yurj26 已提交
2421
console.log(Math.cbrt(1));
Y
yurj26 已提交
2422 2423
// expected output: 1

Y
yurj26 已提交
2424
console.log(Math.cbrt(2));
Y
yurj26 已提交
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
// expected output: 1.2599210498948732
```

#### ceil

Math.ceil() 函数总是四舍五入并返回大于等于给定数字的最小整数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2436
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2437 2438

```ts
Y
yurj26 已提交
2439
console.log(Math.ceil(0.95));
Y
yurj26 已提交
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
// expected output: 1

console.log(Math.ceil(4));
// expected output: 4

console.log(Math.ceil(7.004));
// expected output: 8

console.log(Math.ceil(-7.004));
// expected output: -7

```

#### clz32

Math.clz32() 函数返回一个数字在转换成 32 无符号整形数字的二进制形式后,开头的 0 的个数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2461
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485

```ts
console.log(Math.clz32(1));
// expected output: 31

console.log(Math.clz32(1000));
// expected output: 22

console.log(Math.clz32());
// expected output: 32

console.log(Math.clz32(3.5));
// expected output: 30

```

#### cos

Math.cos() 函数返回一个数值的余弦值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2486
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503

```ts
console.log(Math.cos(0));
// expected output: 1

console.log(Math.cos(1));
// expected output: 0.5403023058681398
```

#### cosh

Math.cosh() 函数返回数值的双曲余弦函数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2504
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524

```ts
console.log(Math.cosh(0));
// expected output: 1

console.log(Math.cosh(1));
// expected output: 1.5430806348152437

console.log(Math.cosh(-1));
// expected output: 1.5430806348152437
```

#### exp

Math.exp() 函数返回 e^x,x 表示参数,e 是欧拉常数(Euler's constant),自然对数的底数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2525
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545

```ts
console.log(Math.exp(-1));
// expected output: 0.36787944117144233

console.log(Math.exp(0));
// expected output: 1

console.log(Math.exp(1));
// expected output: 2.718281828459045
```

#### expm1

Math.expm1() 函数返回 E^x - 1, 其中 x 是该函数的参数,E 是自然对数的底数 2.718281828459045。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2546
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2547 2548 2549

```ts
console.log(Math.expm1(1));
Y
yurj26 已提交
2550
// expected output: 1.718281828459045
Y
yurj26 已提交
2551 2552 2553 2554 2555 2556 2557

console.log(Math.expm1(-38));
// expected output: -1
```

#### floor

Y
yurj26 已提交
2558
Math.floor() 函数总是返回小于等于一个给定数字的最大整数。
Y
yurj26 已提交
2559 2560 2561 2562 2563

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2564
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605

```ts
console.log(Math.floor(5.95));
// expected output: 5

console.log(Math.floor(5.05));
// expected output: 5

console.log(Math.floor(5));
// expected output: 5

console.log(Math.floor(-5.05));
// expected output: -6
```

#### fround

Math.fround() 可以将任意的数字转换为离它最近的单精度浮点数形式的数字。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
|√|x|x|

```ts
console.log(Math.fround(1.5));
// expected output: 1.5

console.log(Math.fround(1.337));
// expected output: 1.3370000123977661
```

#### hypot

Math.hypot() 函数返回所有参数的平方和的平方根。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2606
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629

```ts
console.log(Math.hypot(3, 4));
// expected output: 5

console.log(Math.hypot(5, 12));
// expected output: 13

console.log(Math.hypot(3, 4, 5));
// expected output: 7.0710678118654755

console.log(Math.hypot(-5));
// expected output: 5
```

#### imul

该函数将两个参数分别转换为 32 位整数,相乘后返回 32 位结果。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2630
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647

```ts
console.log(Math.imul(3, 4));
// expected output: 12

console.log(Math.imul(-5, 12));
// expected output: -60
```

#### log

Math.log() 函数返回一个数的自然对数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2648
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665

```ts
console.log(Math.log(1));
// expected output: 0

console.log(Math.log(10));
// expected output: 2.302585092994046
```

#### log10

Math.log10() 函数返回一个数字以 10 为底的对数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2666
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686

```ts
console.log(Math.log10(10));
// expected output: 1

console.log(Math.log10(100));
// expected output: 2

console.log(Math.log10(1));
// expected output: 0
```

#### log1p

Math.log1p() 函数返回一个数字加 1 后的自然对数 (底为 E), 既log(x+1)。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2687
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704

```ts
console.log(Math.log1p(Math.E-1));
// expected output: 1

console.log(Math.log1p(0));
// expected output: 0
```

#### log2

Math.log2() 函数返回一个数字以 2 为底的对数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2705
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719

```ts
console.log(Math.log2(2));
// expected output: 1

console.log(Math.log2(1024));
// expected output: 10

console.log(Math.log2(1));
// expected output: 0
```

#### max

Y
yurj26 已提交
2720
Math.max() 函数返回作为输入参数的最大数字。
Y
yurj26 已提交
2721 2722 2723 2724 2725

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2726
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743

```ts
console.log(Math.max(1, 3, 2));
// expected output: 3

console.log(Math.max(-1, -3, -2));
// expected output: -1
```

#### min

Math.min() 函数返回作为输入参数的数字中最小的一个。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2744
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761

```ts
console.log(Math.min(2, 3, 1));
// expected output: 1

console.log(Math.min(-2, -3, -1));
// expected output: -3
```

#### pow

Math.pow() 函数返回基数(base)的指数(exponent)次幂,即 base^exponent。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2762
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779

```ts
console.log(Math.pow(7, 3));
// expected output: 343

console.log(Math.pow(4, 0.5));
// expected output: 2
```

#### random

Math.random() 函数返回一个浮点数,伪随机数在范围从0 到小于1。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2780
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804

```ts
function getRandomInt(max:number):number {
  return Math.floor(Math.random() * max);
}

console.log(getRandomInt(3));
// expected output: 0, 1 or 2

console.log(getRandomInt(1));
// expected output: 0

console.log(Math.random());
// expected output: a number from 0 to <1
```

#### round

Math.round() 函数返回一个数字四舍五入后最接近的整数。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2805
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822

```ts
console.log(Math.round(20.49));
// expected output: 20

console.log(Math.round(20.5));
// expected output: 21

console.log(Math.random(-20.5));
// expected output: -20

console.log(Math.random(-20.51));
// expected output: -21
```

#### sign

Y
yurj26 已提交
2823
Math.sign() 函数返回一个数字的符号,分别是 1、-1、0,代表的各是正数、负数、零。
Y
yurj26 已提交
2824 2825 2826 2827 2828

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2829
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849

```ts
console.log(Math.sign(3));
// expected output: 1

console.log(Math.sign(-3));
// expected output: -1

console.log(Math.sign(0));
// expected output: 0
```

#### sin

Math.sin() 函数返回一个数值的正弦值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2850
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2851 2852 2853 2854 2855

```ts
console.log(Math.sin(0));
// expected output: 0

Y
yurj26 已提交
2856
console.log(Math.sin(1));
Y
yurj26 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
// expected output: 0.8414709848078965
```

#### sinh

Math.sinh() 函数返回一个数字 (单位为角度) 的双曲正弦值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2868
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885

```ts
console.log(Math.sinh(0));
// expected output: 0

console.log(Math.sinh(1));
// expected output: 1.1752011936438014
```

#### sqrt

Math.sqrt() 函数返回一个数的平方根。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2886
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910

```ts
function calcHypotenuse(a:number, b:number):number {
  return (Math.sqrt((a * a) + (b * b)));
}

console.log(calcHypotenuse(3, 4));
// expected output: 5

console.log(calcHypotenuse(5, 12));
// expected output: 13

console.log(calcHypotenuse(0, 0));
// expected output: 0
```

#### tan

Math.tan() 方法返回一个数值的正切值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2911
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928

```ts
console.log(Math.tan(0));
// expected output: 0

console.log(Math.tan(1));
// expected output: 1.5574077246549023
```

#### tanh

Math.tanh() 函数将会返回一个数的双曲正切函数值。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2929
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949

```ts
console.log(Math.tanh(-1));
// Expected output: -0.7615941559557649

console.log(Math.tanh(0));
// Expected output: 0

console.log(Math.tanh(1));
// Expected output: 0.7615941559557649
```

#### trunc

Math.trunc() 方法会将数字的小数部分去掉,只保留整数部分。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2950
|√|x|√ `(3.7.1+)`|
Y
yurj26 已提交
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962

```ts
console.log(Math.trunc(13.37));
// Expected output: 13

console.log(Math.trunc(42.84));
// Expected output: 42

console.log(Math.trunc(0.123));
// Expected output: 0
```

fxy060608's avatar
fxy060608 已提交
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
### Number

Number 对象是经过封装的能让你处理数字值的对象。

#### 实例方法

#### toFixed

toFixed() 方法使用定点表示法来格式化一个数值。

fxy060608's avatar
fxy060608 已提交
2973 2974
**平台差异说明**

2975
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
2976
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
2977
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
2978

fxy060608's avatar
fxy060608 已提交
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
```ts
function financial(x: Number): String {
  return x.toFixed(2);
}
console.log(financial(123.456));
// expected output: "123.46"
console.log(financial(0.004));
// expected output: "0.00"
```

fxy060608's avatar
fxy060608 已提交
2989
### Set
fxy060608's avatar
fxy060608 已提交
2990

fxy060608's avatar
fxy060608 已提交
2991 2992 2993
Set 对象是值的集合,你可以按照插入的顺序迭代它的元素。Set 中的元素只会出现一次,即 Set 中的元素是唯一的。


fxy060608's avatar
fxy060608 已提交
2994
#### 实例属性
fxy060608's avatar
fxy060608 已提交
2995

fxy060608's avatar
fxy060608 已提交
2996
#### size
fxy060608's avatar
fxy060608 已提交
2997 2998 2999

返回 Set 对象中元素的个数。

fxy060608's avatar
fxy060608 已提交
3000 3001
**平台差异说明**

3002
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3003
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3004
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3005

fxy060608's avatar
fxy060608 已提交
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
```ts
const set1 = new Set<Any>();

set1.add(42);
set1.add('forty two');
set1.add('forty two');

console.log(set1.size);
// expected output: 2
```

fxy060608's avatar
fxy060608 已提交
3017
#### 实例方法
fxy060608's avatar
fxy060608 已提交
3018

fxy060608's avatar
fxy060608 已提交
3019
#### add
fxy060608's avatar
fxy060608 已提交
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033

add() 方法用来向一个 Set 对象的末尾添加一个指定的值。

```ts
const set1 = new Set<number>();
set1.add(42);
set1.add(42);
set1.add(13);
set1.forEach((item)=>{
  console.log(item);
  // expected output: 42
  // expected output: 13  
})
```
fxy060608's avatar
fxy060608 已提交
3034
#### clear
fxy060608's avatar
fxy060608 已提交
3035 3036 3037

clear() 方法用来清空一个 Set 对象中的所有元素。

fxy060608's avatar
fxy060608 已提交
3038 3039
**平台差异说明**

3040
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3041
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3042
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3043

fxy060608's avatar
fxy060608 已提交
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
```ts
const set1 = new Set<any>();
set1.add(1);
set1.add('foo');
console.log(set1.size);
// expected output: 2
set1.clear();
console.log(set1.size);
// expected output: 0
```

fxy060608's avatar
fxy060608 已提交
3055
##### delete
fxy060608's avatar
fxy060608 已提交
3056 3057 3058

delete() 方法可以从一个 Set 对象中删除指定的元素。

fxy060608's avatar
fxy060608 已提交
3059 3060
**平台差异说明**

3061
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3062
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3063
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3064

fxy060608's avatar
fxy060608 已提交
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074
```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');
console.log(map1.delete('bar'));
// expected result: true
// (true indicates successful removal)
console.log(map1.has('bar'));
// expected result: false
```

fxy060608's avatar
fxy060608 已提交
3075
#### forEach
fxy060608's avatar
fxy060608 已提交
3076 3077 3078

forEach 方法会根据集合中元素的插入顺序,依次执行提供的回调函数。

Y
yurj26 已提交
3079 3080 3081 3082 3083 3084 3085 3086 3087
```ts
const set1 = new Set<number>([42, 13]);
set1.forEach((item)=>{
  console.log(item);
  // expected output: 42
  // expected output: 13
})
```

fxy060608's avatar
fxy060608 已提交
3088
#### has
fxy060608's avatar
fxy060608 已提交
3089 3090 3091

has() 方法返回一个布尔值来指示对应的值 value 是否存在 Set 对象中。

fxy060608's avatar
fxy060608 已提交
3092 3093
**平台差异说明**

3094
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3095
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3096
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3097

Y
yurj26 已提交
3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110
```ts
const set1 = new Set<number>([1, 2, 3, 4, 5]);

console.log(set1.has(1));
// expected output: true

console.log(set1.has(5));
// expected output: true

console.log(set1.has(6));
// expected output: false
```

fxy060608's avatar
fxy060608 已提交
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
### String

String 全局对象是一个用于字符串或一个字符序列的构造函数。

字符串字面量采取以下形式:

```ts
'string text'
"string text"
"中文/汉语"
"español"
"English "
"हिन्दी"
"العربية"
"português"
"বাংলা"
"русский"
"日本語"
"ਪੰਜਾਬੀ"
"한국어"
```

#### 实例属性

#### length
length 属性表示一个字符串的长度。
```ts
const x = "Mozilla";
const empty = "";

console.log("Mozilla is " + x.length + " code units long");
/* "Mozilla is 7 code units long" */

console.log("The empty string is has a length of " + empty.length);
/* "The empty string is has a length of 0" */
```

Y
yurj26 已提交
3148 3149 3150 3151 3152 3153 3154
注意事项:

- 在不同平台上,字符串的长度限制不同,超出限制会导致相应的错误或异常
	* 编译至 JavaScript 平台时,最大长度取决于 JavaScript 引擎,例如在 V8 中,最大长度为 2^30 - 25,超出限制会报错:`Invalid string length`
	* 编译至 Kotlin 平台时,最大长度受系统内存的限制,超出限制会报错:`java.lang.OutOfMemoryError: char[] of length xxx would overflow`
  * 编译至 Swift 平台时,最大长度也受系统内存的限制,超出限制目前没有返回信息。

fxy060608's avatar
fxy060608 已提交
3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
#### 实例方法

#### at

at() 方法接受一个整数值,并返回一个新的 String,该字符串由位于指定偏移量处的单个 UTF-16 码元组成。该方法允许正整数和负整数。负整数从字符串中的最后一个字符开始倒数。

```ts
const sentence = 'The quick brown fox jumps over the lazy dog.';
let index = 5;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of 5 the character returned is u"
index = -4;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of -4 the character returned is d"
```

#### charAt

charAt() 方法从一个字符串中返回指定的字符。

```ts
const anyString = "Brave new world";

console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");
// The character at index 0 is 'B'
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
// The character at index 1 is 'r'
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
// The character at index 2 is 'a'
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
// The character at index 3 is 'v'
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
// The character at index 4 is 'e'
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
// The character at index 999 is ''
```

#### charCodeAt

charCodeAt() 方法返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元

```ts
const sentence = 'The quick brown fox jumps over the lazy dog.';
const index = 4;
console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
// expected output: "The character code 113 is equal to q"
```

#### concat

concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。

fxy060608's avatar
fxy060608 已提交
3207 3208
**平台差异说明**

3209
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3210
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3211
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3212

fxy060608's avatar
fxy060608 已提交
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
```ts
let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.
```

#### endsWith

endsWith() 方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。

fxy060608's avatar
fxy060608 已提交
3223 3224
**平台差异说明**

3225
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3226
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3227
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3228

fxy060608's avatar
fxy060608 已提交
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
```ts
const str1 = 'Cats are the best!';
console.log(str1.endsWith('best!'));
// expected output: true
console.log(str1.endsWith('best', 17));
// expected output: true
const str2 = 'Is this a question?';
console.log(str2.endsWith('question'));
// expected output: false
```

#### includes

includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。

fxy060608's avatar
fxy060608 已提交
3244 3245
**平台差异说明**

3246
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3247
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3248
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3249

fxy060608's avatar
fxy060608 已提交
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
```ts
const str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false
```
#### indexOf

indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。

fxy060608's avatar
fxy060608 已提交
3263 3264
**平台差异说明**

3265
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3266
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3267
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3268

fxy060608's avatar
fxy060608 已提交
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
```ts
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"

```
#### padEnd

padEnd() 方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。

fxy060608's avatar
fxy060608 已提交
3286 3287
**平台差异说明**

3288
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3289
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3290
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3291

fxy060608's avatar
fxy060608 已提交
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303
```ts
const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));
// expected output: "Breaded Mushrooms........"
const str2 = '200';
console.log(str2.padEnd(5));
// expected output: "200  "
```
#### padStart

padStart() 方法用另一个字符串填充当前字符串 (如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。

fxy060608's avatar
fxy060608 已提交
3304 3305
**平台差异说明**

3306
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3307
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3308
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3309

fxy060608's avatar
fxy060608 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318
```ts
const str1 = '5';
console.log(str1.padStart(2, '0'));
// expected output: "05"
```
#### repeat

repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。

fxy060608's avatar
fxy060608 已提交
3319 3320
**平台差异说明**

3321
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3322
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3323
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3324

fxy060608's avatar
fxy060608 已提交
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
```ts
"abc".repeat(0)      // ""
"abc".repeat(1)      // "abc"
"abc".repeat(2)      // "abcabc"
"abc".repeat(3.5)    // "abcabcabc" 参数 count 将会被自动转换成整数。
```

#### replace

replace() 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。如果pattern是字符串,则仅替换第一个匹配项。原字符串不会改变。

fxy060608's avatar
fxy060608 已提交
3336 3337
**平台差异说明**

3338
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3339
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3340
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3341

fxy060608's avatar
fxy060608 已提交
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
```ts
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

```
#### search

search() 方法执行正则表达式和 String 对象之间的一个搜索匹配。

fxy060608's avatar
fxy060608 已提交
3356 3357
**平台差异说明**

3358
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3359
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3360
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3361

fxy060608's avatar
fxy060608 已提交
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
```ts
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
// any character that is not a word character or whitespace
const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// expected output: 43
console.log(paragraph[paragraph.search(regex)]);
// expected output: "."
```
#### slice

slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。

fxy060608's avatar
fxy060608 已提交
3375 3376
**平台差异说明**

3377
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3378
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3379
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3380

fxy060608's avatar
fxy060608 已提交
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
```ts
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// expected output: "the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"
```

#### split

split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。

fxy060608's avatar
fxy060608 已提交
3393 3394
**平台差异说明**

3395
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3396
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3397
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3398

fxy060608's avatar
fxy060608 已提交
3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
```ts
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"
const chars = str.split('');
console.log(chars[8]);
// expected output: "k"
```
#### toLowerCase

toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。

fxy060608's avatar
fxy060608 已提交
3413 3414
**平台差异说明**

3415
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3416
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3417
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3418

fxy060608's avatar
fxy060608 已提交
3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
```ts
console.log('中文简体 zh-CN || zh-Hans'.toLowerCase());
// 中文简体 zh-cn || zh-hans
console.log( "ALPHABET".toLowerCase() );
// "alphabet"
```
#### toUpperCase

toUpperCase() 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。

fxy060608's avatar
fxy060608 已提交
3429 3430
**平台差异说明**

3431
|JavaScript|Kotlin|Swift|
fxy060608's avatar
fxy060608 已提交
3432
|:-:|:-:|:-:|
lizhongyi_'s avatar
lizhongyi_ 已提交
3433
|√|√|√ `(3.6.11+)`|
fxy060608's avatar
fxy060608 已提交
3434

fxy060608's avatar
fxy060608 已提交
3435 3436 3437 3438 3439
```ts
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
```
fxy060608's avatar
fxy060608 已提交
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473

### 定时器

#### setTimeout
设定一个定时器。在定时到期以后执行注册的回调函数
```ts
setTimeout(() => {
  console.log("Delayed for 1 second.")
}, 1000)
```
#### clearTimeout
取消由 setTimeout 设置的定时器。
```ts
const timer = setTimeout(() => {
  console.log("Delayed for 1 second.")
}, 1000)
clearTimeout(timer)
```
#### setInterval
设定一个定时器。按照指定的周期(以毫秒计)来执行注册的回调函数
```ts
setInterval(() => {
  console.log(Date.now())
}, 1000)
```
#### clearInterval
取消由 setInterval 设置的定时器。
```ts
const timer = setInterval(() => {
  console.log(Date.now())
}, 1000)
clearInterval(timer)
```

fxy060608's avatar
fxy060608 已提交
3474
## 关键词
fxy060608's avatar
fxy060608 已提交
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495

- `as` 
    * 用于类型转换。
    * 为导入指定一个别名
- `break` 
    * 中止当前循环,switch语句,并把程序控制流转到紧接着被中止语句后面的语句。
- `case` 
    *`switch` 搭配使用。
- `catch` 
    *`try` 搭配使用,捕获程序异常。
- `class` 
    * 声明创建一个新类。
- `const` 
    * 声明一个常量,不能重新赋值。
- `continue` 
    * 声明终止当前循环或标记循环的当前迭代中的语句执行,并在下一次迭代时继续执行循环。
- `debugger` 
    * 调用任何可用的调试功能,例如设置断点。 如果没有调试功能可用,则此语句不起作用。
- `default` 
    *`switch` 搭配,匹配不存在时做的事情,也可以用于 `export` 语句。
- `delete` 
W
wanganxp 已提交
3496
    * 用于删除对象的某个属性;如果没有指向这个属性的引用,那它最终会被释放。(目前仅支持 `Javascript` 平台)
fxy060608's avatar
fxy060608 已提交
3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
- `do` 
    * 创建一个执行指定语句的循环,直到condition值为 false。在执行statement 后检测condition,所以指定的statement至少执行一次。
- `else` 
    *`if` 搭配使用。
- `export` 
    * 用于模块导出。
- `extends` 
    * 用于 `class` 继承。
- `finally` 
    *`try-catch` 搭配使用。
- `for` 
    * 创建一个循环,它包含了三个可选的表达式,这三个表达式被包围在圆括号之中,使用分号分隔,后跟一个用于在循环中执行的语句(通常是一个块语句)。
- `function` 
    * 声明定义一个具有指定参数的函数。
- `if` 
    * 当指定条件为真,if 语句会执行一段语句。如果条件为假,则执行另一段语句。
- `import` 
    * 用于导入由另一个模块导出的绑定。
- `in` 
    * 可在 for 循环中迭代对象。
- `instanceof` 
    * 检测一个值具有指定类型。
- `new` 
    * 创建一个 `class` 实例。
- `return` 
    * 终止函数的执行,并返回一个指定的值给函数调用者。
- `super` 
    * 用于访问和调用一个对象的父对象上的函数。
- `switch` 
    * 评估一个表达式,将表达式的值与case子句匹配,并执行与该情况相关联的语句。
- `this` 
    * 引用当前接收者。
- `throw` 
    * 抛出一个异常。
- `try` ]
    * 捕获一个异常。
- `typeof` 
W
wanganxp 已提交
3534
    * 返回一个字符串,表示未经计算的操作数的类型。(目前仅支持 `Javascript` 平台)
fxy060608's avatar
fxy060608 已提交
3535 3536 3537 3538 3539 3540 3541
- `var` 
    * 声明一个变量,不建议使用。
- `void` 
    * 表示函数没有返回结果。
- `while` 
    * 在某个条件表达式为真的前提下,循环执行指定的一段代码,直到那个表达式不为真时结束循环。
- `with` 
W
wanganxp 已提交
3542
    * 扩展一个语句的作用域链。(目前仅支持 `Javascript` 平台)
fxy060608's avatar
fxy060608 已提交
3543
- `yield` 
W
wanganxp 已提交
3544
    * 用来暂停和恢复一个生成器函数。(目前仅支持 `Javascript` 平台)
fxy060608's avatar
fxy060608 已提交
3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
- `enum`
- `implements`
- `interface`
- `let`
- `package`
- `private`
- `protected`
- `public`
- `static`
- `await`
- `abstract`
- `boolean`
- `byte`
- `char`
- `double`
- `final`
- `float`
- `goto`
- `int`
- `long`
- `native`
- `short`
- `synchronized`
- `transient`
- `volatile`

fxy060608's avatar
fxy060608 已提交
3571
## 操作符
fxy060608's avatar
fxy060608 已提交
3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628

- `+`
    * 相加运算符 (+) 用于对两个操作数进行相加运算。
- `+=`
    * 加法赋值操作符 (+=) 将右操作数的值添加到变量,并将结果分配给该变量。两个操作数的类型确定加法赋值运算符的行为。
- `=`
    * 简单赋值操作符 (=) 用于为变量赋值。赋值表达式本身的值为要赋值的值。
- `&`
    * 按位与运算符 (&) 在两个操作数对应的二进位都为 1 时,该位的结果值才为 1,否则为 0。
- `&=`
    * 按位与赋值运算符(&=)表示两个操作数的二进制,对它们进行按位 AND 运算并将结果分配给变量。
- `~`
    * 按位非运算符(~),反转操作数的位。
- `|`
    * 按位或运算符(|),如果两位之一为 1 则设置每位为 1。
- `|=`
    * 按位或赋值操作符 (|=) 使用二进制表示操作数,进行一次按位或操作并赋值。
- `^`
    * 按位异或运算符(^),如果两位只有一位为 1 则设置每位为 1。
- `^=`
    * 按位异或赋值操作符 (^=) 使用二进制表示操作数,进行一次按位异或操作并赋值。
- `?`
- `--`
    * 自减运算符 (--) 将它的操作数减一,然后返回操作数。
- `/`
    * 除法运算符 (/) 计算了两个操作数的商,左边的数是被除数,右边的是除数。
- `/=`
- `==`
- `>`
    * 当左边操作数大于右边的时候,大于 (>) 运算符返回true,否则返回false。
- `>=`
    * 当左边操作数大于等于右边的时候,大于等于 (>=) 运算符返回true,否则返回false。
- `++`
    * 自增运算符 (++) 将其操作数递增(加 1)并返回一个值。
- `!=`
- `<<`
    * 左移操作符 (<<) 将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。
- `<<=`
    * 左移赋值运算符 (<<=) 将变量向左移动指定数量的位,并将结果赋值给变量。
- `<`
    * 当左边操作数小于右边的时候,小于 (<) 运算符返回true,否则返回false。
- `<=`
    * 当左边操作数小于等于右边的时候,小于等于 (>=) 运算符返回true,否则返回false。
- `&&`
    * 逻辑与
- `&&=`
- `!`
- `??=`
- `||`
    * 逻辑或。
- `||=`
    * 逻辑或赋值(x ||= y)运算仅在 x 为虚值时赋值。
- `*`
    * 乘法运算符 (*) 计算操作数的乘积。
- `*=`
- `??`
- `?.`
fxy060608's avatar
fxy060608 已提交
3629
    * 可选链运算符(?.)允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?. 运算符的功能类似于 . 链式运算符,不同之处在于,在引用为空 (nullish ) (null) 的情况下不会引起错误。
fxy060608's avatar
fxy060608 已提交
3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
- `%`
    * 当一个操作数除以第二个操作数时,取余运算符(%)返回剩余的余数。它与被除数的符号保持一致。
- `%=`
- `>>`
    * 右移操作符 (>>) 是将一个操作数按指定移动的位数向右移动,右边移出位被丢弃,左边移出的空位补符号位(最左边那位)。
- `>>=`
    * 右移赋值运算符 (>>=) 将变量向右移动指定数量的位,并将结果赋值给变量。
- `===`
- `!==`
- `-`
- `-=`
- `>>>`
    * 无符号右移运算符(>>>)(零填充右移)将第一个操作数向右移动指定(二进制)位数。
- `>>>=`

fxy060608's avatar
fxy060608 已提交
3645

fxy060608's avatar
fxy060608 已提交
3646 3647
## 开发指南

fxy060608's avatar
fxy060608 已提交
3648
- [使用 uts 开发 uni-app 原生插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
杜庆泉's avatar
杜庆泉 已提交
3649
- [Android平台uts开发指南](https://uniapp.dcloud.net.cn/plugin/uts-for-android.html)
杜庆泉's avatar
杜庆泉 已提交
3650

fxy060608's avatar
fxy060608 已提交
3651

fxy060608's avatar
fxy060608 已提交
3652 3653
## 学习资料

fxy060608's avatar
fxy060608 已提交
3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
### JavaScript 开发者快速上手 uts

JavaScript 是一门非常灵活的编程语言:

- 没有类型约束,一个变量可能初始化时是字符串,然后又被赋值为数字。
- 因为隐式类型转换的存在,使得变量的类型很难在运行前就确定。
- 基于原型的面向对象编程,原型上的属性或方法可以在运行时被修改。

这种灵活性,一方面使得 JavaScript 蓬勃发展,另一方面也让它的代码质量参差不齐,维护成本高。

而 uts 的类型系统,可以在很大程度上弥补 JavaScript 的缺点。

**uts 是静态类型**

类型系统按照「类型检查的时机」来分类,可以分为动态类型和静态类型。

动态类型是指在运行时才会进行类型检查,这种语言的类型错误往往会导致运行时错误。JavaScript 是一门解释型语言,没有编译阶段,所以它是动态类型,以下这段代码在运行时才会报错:

```js
let foo = 1;
foo.split(' ');
// Uncaught TypeError: foo.split is not a function
// 运行时会报错(foo.split 不是一个函数),造成线上 bug
```

静态类型是指编译阶段就能确定每个变量的类型,这种语言的类型错误往往会导致语法错误。uts 在编译阶段就会进行类型检查,所以 uts 是静态类型,这段 uts 代码在编译阶段就会报错了:

```ts
let foo = 1;
foo.split(' ');
// Property 'split' does not exist on type 'number'.
// 编译时会报错(数字没有 split 方法),无法通过编译
```

大部分 JavaScript 代码只需要经过少量的修改,增加类型批注,就可以变成 uts 代码,这跟 ts 非常接近。

举例:

```js
fxy060608's avatar
fxy060608 已提交
3693
// js 求和两个数字
fxy060608's avatar
fxy060608 已提交
3694 3695 3696 3697 3698 3699 3700 3701
function add(left, right) {
    return left + right;
}
```

补充类型批注后,即可变成 uts 代码

```ts
fxy060608's avatar
fxy060608 已提交
3702
// uts 求和
fxy060608's avatar
fxy060608 已提交
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
function add(left: number, right: number): number {
    return left + right;
}
```

**hello uts**

目前我们可以通过[开发uts插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#_3-%E5%BC%80%E5%8F%91uts%E5%8E%9F%E7%94%9F%E6%8F%92%E4%BB%B6)来学习 uts。