|
| 1 | +--- |
| 2 | +chapter: 2 |
| 3 | +pageNumber: 15 |
| 4 | +description: Types are the kinds of data that can be represented and manipulated in the language. |
| 5 | +--- |
| 6 | + |
| 7 | +# 变量类型 |
| 8 | + |
| 9 | +计算机足够复杂,能使用比数字更复杂的变量。这就是变量类型的作用所在。变量有多种类型,不同语言支持的类型也不尽相同。 |
| 10 | + |
| 11 | +最常见的类型有: |
| 12 | + |
| 13 | +* **Number**:数字可以是整数(如 `1`、`-5`、`100`)或浮点数(如 `3.14`、`-2.5`、`0.01`)。JavaScript 不区分整数与浮点数;两者都被视为数字。 |
| 14 | +* **String**:字符串是由字符组成的序列,可以用单引号(如 `'hello'`)或双引号(如 `"world"`)表示。 |
| 15 | +* **Boolean**:布尔值表示真或假,可写为 `true` 或 `false`(不加引号)。 |
| 16 | +* **Null**:null 类型表示空值,含义是“没有值”。写作 `null`(不加引号)。 |
| 17 | +* **Undefined**:undefined 类型表示尚未被赋值的值。如果一个变量已声明但未被赋值,它就是 `undefined`。 |
| 18 | +* **Object**:对象是属性的集合,每个属性都有名称和对应的值。可以使用花括号(`{}`)创建对象,并以“名称-值”对的方式为其赋予属性。 |
| 19 | +* **Array**:数组是一种特殊的对象类型,用来保存一组条目。可以使用方括号(`[]`)创建数组,并为其赋予一个值列表。 |
| 20 | +* **Function**:函数是一段可以定义并通过名称调用的代码。[函数](../functions/README.md)可以接收参数(输入)并返回一个值(输出)。可以使用 `function` 关键字创建函数。 |
| 21 | + |
| 22 | +JavaScript 是一种“*弱类型*”语言,这意味着你不必显式声明变量的数据类型。只需使用 `var` 关键字表明你在声明变量,解释器会根据上下文与是否使用引号推断你所使用的数据类型。 |
| 23 | + |
| 24 | +{% exercise %} |
| 25 | +声明三个变量并用以下值初始化:将 `age` 设为数字、`name` 设为字符串、`isMarried` 设为布尔值。 |
| 26 | + |
| 27 | +{% initial %} |
| 28 | +let age = |
| 29 | +let name = |
| 30 | +let isMarried = |
| 31 | +{% solution %} |
| 32 | +let age = 30 |
| 33 | +let name = "Cecilia" |
| 34 | +let isMarried = true |
| 35 | + |
| 36 | +{% validation %} |
| 37 | +assert(typeof age === "number" && typeof name === "string" && typeof isMarried === "boolean"); |
| 38 | + |
| 39 | +{% context %} |
| 40 | +{% endexercise %} |
| 41 | + |
| 42 | +`typeof` 运算符用于检查变量的数据类型。 |
| 43 | + |
| 44 | +```javascript |
| 45 | +typeof "John" // 返回 "string" |
| 46 | +typeof 3.14 // 返回 "number" |
| 47 | +typeof NaN // 返回 "number" |
| 48 | +typeof false // 返回 "boolean" |
| 49 | +typeof [1,2,3,4] // 返回 "object" |
| 50 | +typeof {name:'John', age:34} // 返回 "object" |
| 51 | +typeof new Date() // 返回 "object" |
| 52 | +typeof function () {} // 返回 "function" |
| 53 | +typeof myCar // 返回 "undefined" * |
| 54 | +typeof null // 返回 "object |
| 55 | +``` |
| 56 | + |
| 57 | +JavaScript 中使用的数据类型可以按“是否能包含值”分为两类。 |
| 58 | + |
| 59 | +可以包含值的数据类型: |
| 60 | + |
| 61 | +* `string` |
| 62 | +* `number` |
| 63 | +* `boolean` |
| 64 | +* `object` |
| 65 | +* `function` |
| 66 | + |
| 67 | +{% hint style="info" %} |
| 68 | +`Object`、`Date`、`Array`、`String`、`Number` 与 `Boolean` 是 JavaScript 中可用的对象类型。 |
| 69 | +{% endhint %} |
| 70 | + |
| 71 | +不能包含值的数据类型: |
| 72 | + |
| 73 | +* `null` |
| 74 | +* `undefined` |
| 75 | + |
| 76 | +原始(primitive)数据值是没有附加属性与方法、且不是对象的简单数据值。它们是不可变的,也就是不能被就地修改。共有 7 种原始数据类型: |
| 77 | + |
| 78 | +* string |
| 79 | +* number |
| 80 | +* bigint |
| 81 | +* boolean |
| 82 | +* undefined |
| 83 | +* symbol |
| 84 | +* null |
| 85 | + |
| 86 | +{% exercise %} |
| 87 | +声明一个名为 `person` 的变量,并用一个对象初始化它。该对象包含以下属性:`age` 为数字、`name` 为字符串、`isMarried` 为布尔值。 |
| 88 | + |
| 89 | +{% initial %} |
| 90 | +let person = |
| 91 | + |
| 92 | +{% solution %} |
| 93 | +let person = { |
| 94 | +name: "Mary", |
| 95 | +age: 25, |
| 96 | +isMarried: false |
| 97 | +}; |
| 98 | + |
| 99 | +{% validation %} |
| 100 | +assert(typeof person.age === "number" && typeof person.name === "string" && typeof person.isMarried === "boolean"); |
| 101 | + |
| 102 | +{% context %} |
| 103 | +{% endexercise %} |
0 commit comments