From 1ef10032fb161b079e1ab75780b80ce7717d7013 Mon Sep 17 00:00:00 2001 From: sqrtthree Date: Mon, 9 Apr 2018 22:46:17 +0800 Subject: [PATCH 1/5] Transfer the existing translation Co-authored-by: maoxiaoke resolve #5 --- 1-js/02-first-steps/05-types/article.md | 175 ++++++++++++------------ 1 file changed, 88 insertions(+), 87 deletions(-) diff --git a/1-js/02-first-steps/05-types/article.md b/1-js/02-first-steps/05-types/article.md index 9ed9a2d3b0..bf660a4aa8 100644 --- a/1-js/02-first-steps/05-types/article.md +++ b/1-js/02-first-steps/05-types/article.md @@ -1,70 +1,72 @@ -# Data types +# 数据类型 -A variable in JavaScript can contain any data. A variable can at one moment be a string and later receive a numeric value: +JavaScript 中的变量可以保存任何数据。变量在前一刻可以是个字符串,然后又收到一个数值: ```js -// no error +// 没有错误 let message = "hello"; message = 123456; ``` -Programming languages that allow such things are called "dynamically typed", meaning that there are data types, but variables are not bound to any of them. +允许这种操作的编程语言称为“动态类型”(dynamically typed)的编程语言,意思是,拥有数据类型,但是变量并不限于数据类型中的任何一个。 -There are seven basic data types in JavaScript. Here we'll study the basics, and in the next chapters we'll talk about each of them in detail. +在 JavaScript 中有七种基本数据类型。这一章我们会学习基本知识,下一章我们会详细介绍它们。 -## A number +[cut] + +## number 类型 ```js let n = 123; n = 12.345; ``` -The *number* type serves both for integer and floating point numbers. +*number* 类型用于整数和浮点数。 -There are many operations for numbers, e.g. multiplication `*`, division `/`, addition `+`, subtraction `-` and so on. +数字有很多操作,比如,乘法 `*`,除法 `/`,加法 `+`,减法 `-` 等等。 -Besides regular numbers, there are so-called "special numeric values" which also belong to that type: `Infinity`, `-Infinity` and `NaN`. +除了常规的数字,还包括所谓的 “特殊数值” 也属于这种类型:`Infinity`, `-Infinity` 和 `NaN`。 -- `Infinity` represents the mathematical [Infinity](https://en.wikipedia.org/wiki/Infinity) ∞. It is a special value that's greater than any number. +- `Infinity` 代表数学概念中的[无限](https://en.wikipedia.org/wiki/Infinity) ∞。是一个比任何数字都大的特殊值。 - We can get it as a result of division by zero: + 我们可以通过除以0来得到它。 ```js run alert( 1 / 0 ); // Infinity ``` - Or just mention it in the code directly: + 或者在代码中直接提及它。 ```js run alert( Infinity ); // Infinity ``` -- `NaN` represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance: +- `NaN` 代表一个计算错误。它是一个不对的或者一个未定义的数学操作所得到的结果,比如: ```js run - alert( "not a number" / 2 ); // NaN, such division is erroneous + alert( "not a number" / 2 ); // NaN, 这样的除法是错误的 ``` - `NaN` is sticky. Any further operation on `NaN` would give `NaN`: + `NaN` 是粘性的。任何对 `NaN` 的进一步操作都会给出 `NaN`: ```js run alert( "not a number" / 2 + 5 ); // NaN ``` - So, if there's `NaN` somewhere in a mathematical expression, it propagates to the whole result. + 所以,如果在数学表达式中有一个 `NaN`,会被传播到最终结果。 -```smart header="Mathematical operations are safe" -Doing maths is safe in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc. +```smart header="数学运算是安全的" +在 JavaScript 中做数学运算是安全的。我们可以做任何事:除以0,将非数字字符串视为数字,等等。 -The script will never stop with a fatal error ("die"). At worst we'll get `NaN` as the result. +脚本永远不会有致命的错误("死亡")。最坏的情况下,会得到 `NaN` 作为结果。 ``` -Special numeric values formally belong to the "number" type. Of course they are not numbers in a common sense of this word. +特殊的数值属于*number*类型。当然,对这个词的一般认识是,它们并不是数字。 -We'll see more about working with numbers in the chapter . +我们将在章节 了解更多有关使用数字的内容。 -## A string +## string 类型 -A string in JavaScript must be quoted. +JavaScript 中的字符串必须被包含在引号里面。 ```js let str = "Hello"; @@ -72,15 +74,15 @@ let str2 = 'Single quotes are ok too'; let phrase = `can embed ${str}`; ``` -In JavaScript, there are 3 types of quotes. +在 JavaScript 中,有三种包含字符串的方式。 -1. Double quotes: `"Hello"`. -2. Single quotes: `'Hello'`. -3. Backticks: `Hello`. +1. 双引号: `"Hello"`. +2. 单引号: `'Hello'`. +3. 反引号: `Hello`. -Double and single quotes are "simple" quotes. There's no difference between them in JavaScript. +双引号和单引号都是“简单”引用,在 JavaScript 中两者并没有什么差别。 -Backticks are "extended functionality" quotes. They allow us to embed variables and expressions into a string by wrapping them in `${…}`, for example: +反引号是*功能扩展*的引用,允许通过`${…}`, 将变量和表达式嵌入到字符串中。例如: ```js run let name = "John"; @@ -89,78 +91,78 @@ let name = "John"; alert( `Hello, *!*${name}*/!*!` ); // Hello, John! // embed an expression -alert( `the result is *!*${1 + 2}*/!*` ); // the result is 3 +alert( `the result is *!*${1 + 2}*/!*` ); // 结果是 3 ``` -The expression inside `${…}` is evaluated and the result becomes a part of the string. We can put anything there: a variable like `name` or an arithmetical expression like `1 + 2` or something more complex. +`${…}` 内的表达式会被计算,结果成为字符串的一部分。可以在`${…}` 内放置任何东西:诸如 `name` 的变量,或者诸如 `1 + 2` 的算数表达式,或者其他一些更复杂的。 -Please note that this can only be done in backticks. Other quotes do not allow such embedding! +需要注意的是,这仅仅在反引号内有效,其他引号不允许这种嵌入。 ```js run -alert( "the result is ${1 + 2}" ); // the result is ${1 + 2} (double quotes do nothing) +alert( "the result is ${1 + 2}" ); // 结果是 ${1 + 2} (双引号什么也不做) ``` -We'll cover strings more thoroughly in the chapter . +我们会在章节 讨论更多的细节。 -```smart header="There is no *character* type." -In some languages, there is a special "character" type for a single character. For example, in the C language and in Java it is `char`. +```smart header="没有 *character* 类型。" +在一些语言中,单个字符有一个特殊的 “character” 类型,在 C 语言和 Java 语言中是 `char`。 -In JavaScript, there is no such type. There's only one type: `string`. A string may consist of only one character or many of them. +JavaScript 中没有这种类型。只有一种 `string` 类型,一个字符串可以包含一个或多个字符。 ``` -## A boolean (logical type) +## boolean 类型 -The boolean type has only two values: `true` and `false`. +boolean 仅包含两个值:`true` 和 `false`。 -This type is commonly used to store yes/no values: `true` means "yes, correct", and `false` means "no, incorrect". +这种类型通常用于存储表示 yes 或 no 的值:`true` 意味着 "yes,正确",`false` 意味着 "no,不正确"。 -For instance: +比如: ```js let nameFieldChecked = true; // yes, name field is checked let ageFieldChecked = false; // no, age field is not checked ``` -Boolean values also come as a result of comparisons: +布尔值也可作为比较的结果: ```js run let isGreater = 4 > 1; -alert( isGreater ); // true (the comparison result is "yes") +alert( isGreater ); // true (比较的结果是 "yes") ``` -We'll cover booleans more deeply later in the chapter . +更详细的内容将会在章节 进行介绍。 -## The "null" value +## "null" 值 -The special `null` value does not belong to any type of those described above. +特殊的 `null` 值不属于上述任何一种类型。 -It forms a separate type of its own, which contains only the `null` value: +它构成一个独立的类型,只包含 `null` 值: ```js let age = null; ``` -In JavaScript `null` is not a "reference to a non-existing object" or a "null pointer" like in some other languages. +相比较于其他语言,JavaScript 中的`null` 不是一个 “对不存在对象的引用” 或者 “null 指针”。 -It's just a special value which has the sense of "nothing", "empty" or "value unknown". +仅仅是一个含义为“无”、“空”或“值未知”的特殊值。 -The code above states that the `age` is unknown or empty for some reason. +上面的代码表示,由于某些原因,`age` 是未知的。 -## The "undefined" value +## "undefined" 值 -The special value `undefined` stands apart. It makes a type of its own, just like `null`. +特殊值和 `null` 一样,自成类型。 -The meaning of `undefined` is "value is not assigned". +`undefined` 的含义是 `未被赋值`。 -If a variable is declared, but not assigned, then its value is exactly `undefined`: +如果变量被声明,而未被赋值,那么它的值就是 `undefined`: ```js run let x; -alert(x); // shows "undefined" +alert(x); // 弹出 "undefined" ``` -Technically, it is possible to assign `undefined` to any variable: +原理上来说,可以为任何变量赋值为 `undefined`: ```js run let x = 123; @@ -170,28 +172,28 @@ x = undefined; alert(x); // "undefined" ``` -...But it's not recommended to do that. Normally, we use `null` to write an "empty" or an "unknown" value into the variable, and `undefined` is only used for checks, to see if the variable is assigned or similar. +...但是不建议这样做。通常,使用使用 `null` 将一个“空”或者“未知”的值写入变量中,`undefined` 仅仅用于检验,以查看变量是否被赋值或者其他类似的操作。 -## Objects and Symbols +## object 类型和 symbol 类型 -The `object` type is special. +`object` 类型是特殊的类型。 -All other types are called "primitive", because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities. We'll deal with them later in the chapter after we know enough about primitives. +其他所有的类型都称为“原始类型”,因为它们的值只包含一个单独的东西(字符串、数字或者其他)。相反,对象用于储存数据集合和更复杂的实体。在充分了解原始类型之后,我们会在章节 介绍对象。 -The `symbol` type is used to create unique identifiers for objects. We have to mention it here for completeness, but it's better to study them after objects. +`symbol` 类型用于创建对象的唯一标识符。为了学习的完整性,我们在这里提到 `symbol` 类型,但最好在学完对象之后再学习它。 -## The typeof operator [#type-typeof] +## typeof 运算符 [#type-typeof] -The `typeof` operator returns the type of the argument. It's useful when we want to process values of different types differently, or just want to make a quick check. +`typeof` 运算符返回参数的类型。当我们想要分别处理不同类型值的时候,或者简单地进行检验,就很有用。 -It supports two forms of syntax: +它支持两种语法形式: -1. As an operator: `typeof x`. -2. Function style: `typeof(x)`. +1. 作为运算符: `typeof x`. +2. 函数形式: `typeof(x)`. -In other words, it works both with parentheses or without them. The result is the same. +换言之,有括号和没有括号,结果是一样的。 -The call to `typeof x` returns a string with the type name: +对 `typeof x` 的调用返回数据类型的字符串。 ```js typeof undefined // "undefined" @@ -217,29 +219,28 @@ typeof alert // "function" (3) */!* ``` -The last three lines may need additional explanations: - -1. `Math` is a built-in object that provides mathematical operations. We will learn it in the chapter . Here it serves just as an example of an object. -2. The result of `typeof null` is `"object"`. That's wrong. It is an officially recognized error in `typeof`, kept for compatibility. Of course, `null` is not an object. It is a special value with a separate type of its own. So, again, that's an error in the language. -3. The result of `typeof alert` is `"function"`, because `alert` is a function of the language. We'll study functions in the next chapters, and we'll see that there's no special "function" type in the language. Functions belong to the object type. But `typeof` treats them differently. Formally, it's incorrect, but very convenient in practice. +最后三行可能需要额外的说明: +1. `Math` 是一个提供数学运算的内建对象。我们会在章节 学习它。此处作为一个对象的例子。 +2. `typeof null` 的结果是 `"object"`。这是不对的。这是一个官方承认的错误,为了兼容性而保留。当然,`null` 不是一个对象。它有自己的类型,是一个特殊值。再次强调,这是语言中的一个错误。 +3. `typeof alert` 的结果是 `"function"`,因为 `alert` 在语言中是一个函数。我们会在下一章学习函数,那时我们会了解到,在语言中没有一个特别的 “function” 类型。函数隶属于 object 类型。但是 `typeof` 对函数区分对待。这不正确,但在实践中非常方便。 -## Summary +## 总结 -There are 7 basic types in JavaScript. +JavaScript 中有七种基本的类型。 -- `number` for numbers of any kind: integer or floating-point. -- `string` for strings. A string may have one or more characters, there's no separate single-character type. -- `boolean` for `true`/`false`. -- `null` for unknown values -- a standalone type that has a single value `null`. -- `undefined` for unassigned values -- a standalone type that has a single value `undefined`. -- `object` for more complex data structures. -- `symbol` for unique identifiers. +- `number` 用于任何类型的数字: 整数或者浮点数。 +- `string` 用于字符串。一个字符串可以包含一个或多个字符,所以没有单独的单字符类型。 +- `boolean` 用于 `true` 和 `false`。 +- `null` 用于未知的值 -- 只有一个 `null` 值的独立类型。 +- `undefined` 用于未定义的值 -- 只有一个 `undefined` 值的独立类型。 +- `object` 用于更复杂的数据结构。 +- `symbol` 用于唯一的标识符。 -The `typeof` operator allows us to see which type is stored in the variable. +`typeof` 运算符可以查看变量的类型。 -- Two forms: `typeof x` or `typeof(x)`. -- Returns a string with the name of the type, like `"string"`. -- For `null` returns `"object"` -- that's an error in the language, it's not an object in fact. +- 两种形式: `typeof x` 或者 `typeof(x)`。 +- 返回的类型的字符串,比如 `"string"`。 +- `null` 返回 `"object"` -- 这是语言中的一个错误,实际上它并不是一个对象。 -In the next chapters we'll concentrate on primitive values and once we're familiar with them, then we'll move on to objects. +在接下来的章节中,我们将重点介绍原始类型值,一旦掌握了,我们将继续讨论对象。 From 34d30755ba05e8a319c695c6156207daabd71c67 Mon Sep 17 00:00:00 2001 From: sqrtthree Date: Tue, 17 Apr 2018 14:36:17 +0800 Subject: [PATCH 2/5] fix(types): Adjust some contents --- 1-js/02-first-steps/05-types/article.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/1-js/02-first-steps/05-types/article.md b/1-js/02-first-steps/05-types/article.md index bf660a4aa8..4f282e496f 100644 --- a/1-js/02-first-steps/05-types/article.md +++ b/1-js/02-first-steps/05-types/article.md @@ -12,8 +12,6 @@ message = 123456; 在 JavaScript 中有七种基本数据类型。这一章我们会学习基本知识,下一章我们会详细介绍它们。 -[cut] - ## number 类型 ```js @@ -21,15 +19,15 @@ let n = 123; n = 12.345; ``` -*number* 类型用于整数和浮点数。 +**number** 类型用于整数和浮点数。 数字有很多操作,比如,乘法 `*`,除法 `/`,加法 `+`,减法 `-` 等等。 除了常规的数字,还包括所谓的 “特殊数值” 也属于这种类型:`Infinity`, `-Infinity` 和 `NaN`。 -- `Infinity` 代表数学概念中的[无限](https://en.wikipedia.org/wiki/Infinity) ∞。是一个比任何数字都大的特殊值。 +- `Infinity` 代表数学概念中的[无穷大](https://en.wikipedia.org/wiki/Infinity) ∞。是一个比任何数字都大的特殊值。 - 我们可以通过除以0来得到它。 + 我们可以通过除以 0 来得到它。 ```js run alert( 1 / 0 ); // Infinity @@ -55,12 +53,12 @@ n = 12.345; 所以,如果在数学表达式中有一个 `NaN`,会被传播到最终结果。 ```smart header="数学运算是安全的" -在 JavaScript 中做数学运算是安全的。我们可以做任何事:除以0,将非数字字符串视为数字,等等。 +在 JavaScript 中做数学运算是安全的。我们可以做任何事:除以 0,将非数字字符串视为数字,等等。 脚本永远不会有致命的错误("死亡")。最坏的情况下,会得到 `NaN` 作为结果。 ``` -特殊的数值属于*number*类型。当然,对这个词的一般认识是,它们并不是数字。 +特殊的数值属于 **number** 类型。当然,对这个词的一般认识是,它们并不是数字。 我们将在章节 了解更多有关使用数字的内容。 @@ -109,9 +107,9 @@ alert( "the result is ${1 + 2}" ); // 结果是 ${1 + 2} (双引号什么也不 JavaScript 中没有这种类型。只有一种 `string` 类型,一个字符串可以包含一个或多个字符。 ``` -## boolean 类型 +## boolean 类型(逻辑类型) -boolean 仅包含两个值:`true` 和 `false`。 +boolean 类型仅包含两个值:`true` 和 `false`。 这种类型通常用于存储表示 yes 或 no 的值:`true` 意味着 "yes,正确",`false` 意味着 "no,不正确"。 @@ -142,7 +140,7 @@ alert( isGreater ); // true (比较的结果是 "yes") let age = null; ``` -相比较于其他语言,JavaScript 中的`null` 不是一个 “对不存在对象的引用” 或者 “null 指针”。 +相比较于其他语言,JavaScript 中的 `null` 不是一个“对不存在对象的引用”或者 “null 指针”。 仅仅是一个含义为“无”、“空”或“值未知”的特殊值。 From d3704412bcc441e6a79da4a3c2ec590c2ab9236b Mon Sep 17 00:00:00 2001 From: sqrtthree Date: Tue, 17 Apr 2018 14:39:48 +0800 Subject: [PATCH 3/5] fix(types): translate tasks and solutions --- 1-js/02-first-steps/05-types/1-string-quotes/solution.md | 9 ++++----- 1-js/02-first-steps/05-types/1-string-quotes/task.md | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/1-js/02-first-steps/05-types/1-string-quotes/solution.md b/1-js/02-first-steps/05-types/1-string-quotes/solution.md index 68a13c15b2..ff82ebebd4 100644 --- a/1-js/02-first-steps/05-types/1-string-quotes/solution.md +++ b/1-js/02-first-steps/05-types/1-string-quotes/solution.md @@ -1,15 +1,14 @@ - -Backticks embed the expression inside `${...}` into the string. +在反引号字符串中嵌入表达式 `${...}`。 ```js run let name = "Ilya"; -// the expression is a number 1 +// 表达式为数字 1 alert( `hello ${1}` ); // hello 1 -// the expression is a string "name" +// 表达式为一个字符串 "name" alert( `hello ${"name"}` ); // hello name -// the expression is a variable, embed it +// 表达式是一个变量,嵌入进去。 alert( `hello ${name}` ); // hello Ilya ``` diff --git a/1-js/02-first-steps/05-types/1-string-quotes/task.md b/1-js/02-first-steps/05-types/1-string-quotes/task.md index 14ea6b4d66..e1ac37466c 100644 --- a/1-js/02-first-steps/05-types/1-string-quotes/task.md +++ b/1-js/02-first-steps/05-types/1-string-quotes/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# String quotes +# 字符串的引号 -What is the output of the script? +下面的脚本会输出什么? ```js let name = "Ilya"; @@ -14,4 +14,4 @@ alert( `hello ${1}` ); // ? alert( `hello ${"name"}` ); // ? alert( `hello ${name}` ); // ? -``` \ No newline at end of file +``` From 9ee1e2d252645681c851e0650c426e0c7eafc345 Mon Sep 17 00:00:00 2001 From: sqrtthree Date: Tue, 24 Apr 2018 12:05:16 +0800 Subject: [PATCH 4/5] fix: Adjust some punctuation marks due to code review changes --- 1-js/02-first-steps/05-types/article.md | 40 ++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/1-js/02-first-steps/05-types/article.md b/1-js/02-first-steps/05-types/article.md index 4f282e496f..d79b1464da 100644 --- a/1-js/02-first-steps/05-types/article.md +++ b/1-js/02-first-steps/05-types/article.md @@ -21,9 +21,9 @@ n = 12.345; **number** 类型用于整数和浮点数。 -数字有很多操作,比如,乘法 `*`,除法 `/`,加法 `+`,减法 `-` 等等。 +数字有很多操作,比如,乘法 `*`、除法 `/`、加法 `+`、减法 `-` 等等。 -除了常规的数字,还包括所谓的 “特殊数值” 也属于这种类型:`Infinity`, `-Infinity` 和 `NaN`。 +除了常规的数字,还包括所谓的“特殊数值”也属于这种类型:`Infinity`、`-Infinity` 和 `NaN`。 - `Infinity` 代表数学概念中的[无穷大](https://en.wikipedia.org/wiki/Infinity) ∞。是一个比任何数字都大的特殊值。 @@ -55,7 +55,7 @@ n = 12.345; ```smart header="数学运算是安全的" 在 JavaScript 中做数学运算是安全的。我们可以做任何事:除以 0,将非数字字符串视为数字,等等。 -脚本永远不会有致命的错误("死亡")。最坏的情况下,会得到 `NaN` 作为结果。 +脚本永远不会有致命的错误(“死亡”)。最坏的情况下,会得到 `NaN` 作为结果。 ``` 特殊的数值属于 **number** 类型。当然,对这个词的一般认识是,它们并不是数字。 @@ -80,7 +80,7 @@ let phrase = `can embed ${str}`; 双引号和单引号都是“简单”引用,在 JavaScript 中两者并没有什么差别。 -反引号是*功能扩展*的引用,允许通过`${…}`, 将变量和表达式嵌入到字符串中。例如: +反引号是**功能扩展**的引用,允许通过 `${…}`,将变量和表达式嵌入到字符串中。例如: ```js run let name = "John"; @@ -92,7 +92,7 @@ alert( `Hello, *!*${name}*/!*!` ); // Hello, John! alert( `the result is *!*${1 + 2}*/!*` ); // 结果是 3 ``` -`${…}` 内的表达式会被计算,结果成为字符串的一部分。可以在`${…}` 内放置任何东西:诸如 `name` 的变量,或者诸如 `1 + 2` 的算数表达式,或者其他一些更复杂的。 +`${…}` 内的表达式会被计算,结果成为字符串的一部分。可以在 `${…}` 内放置任何东西:诸如 `name` 的变量,或者诸如 `1 + 2` 的算数表达式,或者其他一些更复杂的。 需要注意的是,这仅仅在反引号内有效,其他引号不允许这种嵌入。 ```js run @@ -101,7 +101,7 @@ alert( "the result is ${1 + 2}" ); // 结果是 ${1 + 2} (双引号什么也不 我们会在章节 讨论更多的细节。 -```smart header="没有 *character* 类型。" +```smart header="没有 **character** 类型。" 在一些语言中,单个字符有一个特殊的 “character” 类型,在 C 语言和 Java 语言中是 `char`。 JavaScript 中没有这种类型。只有一种 `string` 类型,一个字符串可以包含一个或多个字符。 @@ -111,7 +111,7 @@ JavaScript 中没有这种类型。只有一种 `string` 类型,一个字符 boolean 类型仅包含两个值:`true` 和 `false`。 -这种类型通常用于存储表示 yes 或 no 的值:`true` 意味着 "yes,正确",`false` 意味着 "no,不正确"。 +这种类型通常用于存储表示 yes 或 no 的值:`true` 意味着 “yes,正确”,`false` 意味着 “no,不正确”。 比如: @@ -130,7 +130,7 @@ alert( isGreater ); // true (比较的结果是 "yes") 更详细的内容将会在章节 进行介绍。 -## "null" 值 +## “null” 值 特殊的 `null` 值不属于上述任何一种类型。 @@ -146,7 +146,7 @@ let age = null; 上面的代码表示,由于某些原因,`age` 是未知的。 -## "undefined" 值 +## “undefined” 值 特殊值和 `null` 一样,自成类型。 @@ -176,7 +176,7 @@ alert(x); // "undefined" `object` 类型是特殊的类型。 -其他所有的类型都称为“原始类型”,因为它们的值只包含一个单独的东西(字符串、数字或者其他)。相反,对象用于储存数据集合和更复杂的实体。在充分了解原始类型之后,我们会在章节 介绍对象。 +其他所有的类型都称为“原生类型”,因为它们的值只包含一个单独的东西(字符串、数字或者其他)。相反,对象用于储存数据集合和更复杂的实体。在充分了解原生类型之后,我们会在章节 介绍对象。 `symbol` 类型用于创建对象的唯一标识符。为了学习的完整性,我们在这里提到 `symbol` 类型,但最好在学完对象之后再学习它。 @@ -186,8 +186,8 @@ alert(x); // "undefined" 它支持两种语法形式: -1. 作为运算符: `typeof x`. -2. 函数形式: `typeof(x)`. +1. 作为运算符:`typeof x`。 +2. 函数形式:`typeof(x)`。 换言之,有括号和没有括号,结果是一样的。 @@ -220,25 +220,25 @@ typeof alert // "function" (3) 最后三行可能需要额外的说明: 1. `Math` 是一个提供数学运算的内建对象。我们会在章节 学习它。此处作为一个对象的例子。 -2. `typeof null` 的结果是 `"object"`。这是不对的。这是一个官方承认的错误,为了兼容性而保留。当然,`null` 不是一个对象。它有自己的类型,是一个特殊值。再次强调,这是语言中的一个错误。 -3. `typeof alert` 的结果是 `"function"`,因为 `alert` 在语言中是一个函数。我们会在下一章学习函数,那时我们会了解到,在语言中没有一个特别的 “function” 类型。函数隶属于 object 类型。但是 `typeof` 对函数区分对待。这不正确,但在实践中非常方便。 +2. `typeof null` 的结果是 `"object"`。这是不对的。这是官方在 `typeof` 方面承认的错误,只是为了兼容性而保留。当然,`null` 不是一个对象。它有自己的类型,是一个特殊值。再次强调,这是语言中的一个错误。 +3. `typeof alert` 的结果是 `"function"`,因为 `alert` 在语言中是一个函数。我们会在下一章学习函数,那时我们会了解到,在语言中没有一个特别的 “function” 类型。函数隶属于 object 类型。但是 `typeof` 会对函数区分对待。这不正确,但在实践中非常方便。 ## 总结 JavaScript 中有七种基本的类型。 -- `number` 用于任何类型的数字: 整数或者浮点数。 +- `number` 用于任何类型的数字:整数或者浮点数。 - `string` 用于字符串。一个字符串可以包含一个或多个字符,所以没有单独的单字符类型。 - `boolean` 用于 `true` 和 `false`。 -- `null` 用于未知的值 -- 只有一个 `null` 值的独立类型。 -- `undefined` 用于未定义的值 -- 只有一个 `undefined` 值的独立类型。 +- `null` 用于未知的值 —— 只有一个 `null` 值的独立类型。 +- `undefined` 用于未定义的值 —— 只有一个 `undefined` 值的独立类型。 - `object` 用于更复杂的数据结构。 - `symbol` 用于唯一的标识符。 `typeof` 运算符可以查看变量的类型。 -- 两种形式: `typeof x` 或者 `typeof(x)`。 +- 两种形式:`typeof x` 或者 `typeof(x)`。 - 返回的类型的字符串,比如 `"string"`。 -- `null` 返回 `"object"` -- 这是语言中的一个错误,实际上它并不是一个对象。 +- `null` 返回 `"object"` —— 这是语言中的一个错误,实际上它并不是一个对象。 -在接下来的章节中,我们将重点介绍原始类型值,一旦掌握了,我们将继续讨论对象。 +在接下来的章节中,我们将重点介绍原生类型值,一旦掌握了,我们将继续讨论对象。 From 1c750ecf145aef59943d9b600761fc579390dead Mon Sep 17 00:00:00 2001 From: LeviDing Date: Tue, 24 Apr 2018 14:52:54 +0800 Subject: [PATCH 5/5] Update article.md --- 1-js/02-first-steps/05-types/article.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1-js/02-first-steps/05-types/article.md b/1-js/02-first-steps/05-types/article.md index d79b1464da..e4041b1b8a 100644 --- a/1-js/02-first-steps/05-types/article.md +++ b/1-js/02-first-steps/05-types/article.md @@ -19,7 +19,7 @@ let n = 123; n = 12.345; ``` -**number** 类型用于整数和浮点数。 +*number* 类型用于整数和浮点数。 数字有很多操作,比如,乘法 `*`、除法 `/`、加法 `+`、减法 `-` 等等。 @@ -130,7 +130,7 @@ alert( isGreater ); // true (比较的结果是 "yes") 更详细的内容将会在章节 进行介绍。 -## “null” 值 +## "null" 值 特殊的 `null` 值不属于上述任何一种类型。 @@ -146,7 +146,7 @@ let age = null; 上面的代码表示,由于某些原因,`age` 是未知的。 -## “undefined” 值 +## "undefined" 值 特殊值和 `null` 一样,自成类型。 @@ -221,7 +221,7 @@ typeof alert // "function" (3) 1. `Math` 是一个提供数学运算的内建对象。我们会在章节 学习它。此处作为一个对象的例子。 2. `typeof null` 的结果是 `"object"`。这是不对的。这是官方在 `typeof` 方面承认的错误,只是为了兼容性而保留。当然,`null` 不是一个对象。它有自己的类型,是一个特殊值。再次强调,这是语言中的一个错误。 -3. `typeof alert` 的结果是 `"function"`,因为 `alert` 在语言中是一个函数。我们会在下一章学习函数,那时我们会了解到,在语言中没有一个特别的 “function” 类型。函数隶属于 object 类型。但是 `typeof` 会对函数区分对待。这不正确,但在实践中非常方便。 +3. `typeof alert` 的结果是 `"function"`,因为 `alert` 在语言中是一个函数。我们会在下一章学习函数,那时我们会了解到,在语言中没有一个特别的 "function" 类型。函数隶属于 object 类型。但是 `typeof` 会对函数区分对待。这不正确,但在实践中非常方便。 ## 总结