提交 8da008c3 编写于 作者: W wizardforcel

2020-06-06 16:22:08

上级 91998838
......@@ -2,7 +2,7 @@
> 原文: [https://howtodoinjava.com/typescript/spread-operator/](https://howtodoinjava.com/typescript/spread-operator/)
在 TypeScript 中,可以使用展开运算符(以省略号的形式)来初始化另一个数组或对象中的数组和对象。 您还可以使用散布运算符进行对象分解。
在 TypeScript 中,可以使用展开运算符(以省略号的形式)来初始化另一个数组或对象中的数组和对象。 您还可以使用展开运算符进行对象分解。
## 展开运算符与`apply()`方法
......@@ -30,7 +30,7 @@ console.log(...argsArray); //1 2 3 4 5
1. #### 初始化数组另一个数组
您可以使用展开运算符以给定的方式从现有阵列创建阵列
您可以使用展开运算符以给定的方式从现有数组创建数组
```java
let origArrayOne = [ 1, 2, 3]; //1,2,3
......
......@@ -59,9 +59,9 @@ scores.push( 'abc' ); //data.ts(24,14): error TS2345: Argument of type '"abc"' i
## 遍历数组
您可以使用`for...of`循环或传统的 for 循环来迭代数组元素。
您可以使用`for...of`循环或传统的`for`循环来迭代数组元素。
#### 使用“ for…of”循环
#### 使用“`for…of`”循环
```java
let scores :number[] = [10, 20, 30, 40];
......@@ -72,9 +72,9 @@ for (var score of scores) {
```
DO NOT use “for…in” loop which is used to iterate over object attributes.
请勿使用用于循环访问对象属性的“`for…in`”循环。
#### 使用传统的 for 循环
#### 使用传统的`for`循环
```java
let scores :number[] = [10, 20, 30, 40];
......@@ -87,7 +87,7 @@ for (var i = 0; i < scores.length; i++) {
## 克隆数组
使用[展开运算符](https://howtodoinjava.com/typescript/spread-operator/)克隆阵列。 这是最简单和推荐的方法。
使用[展开运算符](https://howtodoinjava.com/typescript/spread-operator/)克隆数组。 这是最简单和推荐的方法。
```java
let origScores :number[] = [10, 20, 30];
......@@ -103,9 +103,9 @@ console.log(clonedScores); //[10, 20, 30] is "unchanged"
```
## 合并阵列
## 合并数组
也可以使用[展开运算符](https://howtodoinjava.com/typescript/spread-operator/)合并阵列。 这是最简单推荐的方法。
也可以使用[展开运算符](https://howtodoinjava.com/typescript/spread-operator/)合并数组。 这是最简单推荐的方法。
```java
let scores1 :number[] = [10, 20, 30];
......
......@@ -2,7 +2,7 @@
> 原文: [https://howtodoinjava.com/typescript/enums/](https://howtodoinjava.com/typescript/enums/)
在 TypeScript 中,**枚举**是一组命名常量。 尽管它是可选的,但枚举应为**命名的一组相关常量**。 TypeScript 支持传统的枚举和基于字符串的枚举。
在 TypeScript 中,**枚举**是一组命名常量。 尽管它是可选的,但枚举应为**一组命名的相关常量**。 TypeScript 支持传统的枚举和基于字符串的枚举。
## 1)基于字符串的枚举
......@@ -23,7 +23,7 @@ enum AppStatus {
```
查看 JavaScript 中**生成的代码。 它具有一个生成的查找表,如下所示。**
查看 JavaScript 中**生成的代码**。 它具有一个生成的查找表,如下所示。
```java
var AppStatus;
......@@ -124,7 +124,7 @@ console.log(AppStatus[0]); //ACTIVE
#### 2.3)枚举作为函数参数
To pass enum in functions, declare the argument type of enum itself.
要在函数中传递枚举,请声明枚举本身的参数类型。
```java
enum AppStatus {
......
# TypeScript 映射
# TypeScript 映射
> 原文: [https://howtodoinjava.com/typescript/maps/](https://howtodoinjava.com/typescript/maps/)
......@@ -15,11 +15,11 @@ let myMap = new Map();
## 从映射添加/检索/删除条目
1. `map.set()` –在`Map`中添加条目的方法。
2. `map.get()` –从`Map`检索条目。
3. `map.has()``Map`中存在一个条目。
4. `map.delete()` –从`Map`删除条目。
5. `map.size`'size'属性将返回`Map`的大小。
1. `map.set()` `Map`中添加条目的方法。
2. `map.get()` `Map`检索条目。
3. `map.has()``Map`中存在条目。
4. `map.delete()` `Map`删除条目。
5. `map.size` 将返回`Map`的大小。
```java
let nameAgeMapping = new Map();
......@@ -47,7 +47,7 @@ nameAgeMapping.clear(); //Clear all entries
```
## 遍历 Map 条目
## 遍历`Map`条目
使用`'for...of'`循环可迭代映射键,值或条目。
......
# 在 TypeScript 中设置
# TypeScript 集合
> 原文: [https://howtodoinjava.com/typescript/sets/](https://howtodoinjava.com/typescript/sets/)
`Set`是 ES6 中引入的新数据结构,类似于`Map`。 它允许您将不同的值存储到类似于其他编程语言的列表中 Java,C#。
## 创建集
## 创建集
使用`Set`类型和`new`关键字在 TypeScript 中创建`Set`
......@@ -13,12 +13,12 @@ let mySet = new Set();
```
## 从集中添加/检索/删除值
## 从集中添加/检索/删除值
1. `set.add()` –在`Set`中添加值的方法。
2. `set.has()` –检查`Set`中是否存在值。
3. `set.delete()` –从`Set`中删除一个值。
4. `set.size``size`’属性将返回`Set`的大小。
1. `set.add()` `Set`中添加值的方法。
2. `set.has()` 检查`Set`中是否存在值。
3. `set.delete()` `Set`中删除一个值。
4. `set.size` 将返回`Set`的大小。
```java
let diceEntries = new Set();
......@@ -44,7 +44,7 @@ diceEntries.clear(); //Clear all entries
```
## 遍历 Set
## 遍历集合
使用`'for...of'`循环迭代`Set`值。
......
# TypeScript 函数– REST,可选和默认参数
# TypeScript 函数 – 剩余,可选和默认参数
> 原文: [https://howtodoinjava.com/typescript/functions-rest-optional-default-params/](https://howtodoinjava.com/typescript/functions-rest-optional-default-params/)
学习使用示例创建函数,函数类型声明,可选参数,默认参数和 rest 参数。
学习使用示例创建函数,函数类型声明,可选参数,默认参数和剩余参数。
```java
Table of Contents
......@@ -14,13 +14,13 @@ Default Parameters
Rest Parameters
```
## 创建功能
## 创建函数
在 TypeScript 中,可以通过两种方式创建函数。
1. #### 功能声明
1. #### 函数声明
这些被命名为以传统 JavaScript 风格编写的函数。
这些是以传统 JavaScript 风格编写的命名函数。
```java
console.log( showMyName("Lokesh") ); // Hi! Lokesh
......@@ -33,7 +33,7 @@ Rest Parameters
2. #### 函数表达式
这些功能没有名称。 他们被分配给
这些函数没有名称。 他们被分配给:
```java
console.log(showMyName("Lokesh")); //Error - Define functional expression first.
......@@ -46,9 +46,9 @@ Rest Parameters
```
Please note that **both function declarations looks similar BUT they are not**. The JavaScript interpreter can evaluate a function declaration as it is being parsed ([variable hoisting](https://howtodoinjava.com/typescript/javascript-variable-hoisting/)). On the other hand, the function expression is part of an assignment and will not be evaluated until the assignment has been completed.
请注意,**这两个函数声明看上去都相似,但两者却不相同**。 JavaScript 解释器可以在解析函数声明时对其进行求值([变量提升](https://howtodoinjava.com/typescript/javascript-variable-hoisting/))。 另一方面,函数表达式是赋值的一部分,直到赋值完成后才进行计算。
## 功能类型
## 函数类型
在 TypeScript 中,一切都是类型,函数也是类型。 您可以将变量的类型声明为`Function`,如下所示。
......@@ -67,7 +67,7 @@ let showMyName: Function = function(name: string): string {
与 JavaScript 不同,如果我们尝试调用一个函数而不提供其签名声明的确切数量和参数类型,则 TypeScript 编译器将引发错误。
要解决此问题,可以使用带问号((HTG0])的可选参数。 在下面的示例中,`message`被标记为可选参数。
要解决此问题,可以使用带问号(`?`)的可选参数。 在下面的示例中,`message`被标记为可选参数。
```java
let showMyName = function(name: string, message?: string): string {
......@@ -80,7 +80,7 @@ showMyName('Lokesh', 'How are you?'); //Hi! Lokesh How are you?
```
It is important to note that the **optional parameters must always be located after the required parameters** in the function’s parameter list.
重要的是要注意,**可选参数必须始终位于函数的参数列表中的必需参数之后**
## 默认参数
......@@ -98,17 +98,18 @@ showMyName('Lokesh', 'How are you buddy?'); //Hi! Lokesh How are you buddy?
```
1) Note that same as optional parameters, **default parameters must also be located after the required parameters** in the function’s parameter list.
2) You **cannot make any parameter optional and default both**. Only one type is allowed.
1)请注意,与可选参数一样,**默认参数也必须位于函数参数列表中的必需参数之后**
## REST 参数
2)您**不能使任何参数可选并且默认**。 只允许一种类型。
## 剩余参数
有时,您可能希望创建可以具有不确定数量参数的函数。 对于一个或两个参数,可以使用可选参数或默认参数。 但是,如果参数数量未知,或者在运行时可能有所不同,该怎么办。
例如 您正在读取输入字段值,并且该值可能会因动态 UI 而有所不同。 此处**其余参数**将为您提供帮助。 REST 参数语法使我们可以将**不确定数量的参数表示为数组**
例如您正在读取输入字段值,并且该值可能会因动态 UI 而有所不同。 此处**剩余参数**将为您提供帮助。 剩余参数语法使我们可以将**不确定数量的参数表示为数组**
1. 要创建 REST 参数,请在变量名称前使用省略号,即三个点(`'...'`)。
2. REST 参数必须为数组类型,否则我们将收到编译错误。
1. 要创建剩余参数,请在变量名称前使用省略号,即三个点(`'...'`)。
2. 剩余参数必须为数组类型,否则我们将收到编译错误。
3. 从理论上讲,对最大参数个数没有具体限制。
```java
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册