From d73ea15933dcdc0d0c487659916b688a2903b6f0 Mon Sep 17 00:00:00 2001 From: da Date: Tue, 1 May 2018 19:19:50 -0500 Subject: [PATCH 01/15] Update solution.md --- .../1-string-new-property/solution.md | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md index a169f7769e..e5faaed59d 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md @@ -1,5 +1,5 @@ -Try running it: +试试运行: ```js run let str = "Hello"; @@ -9,23 +9,22 @@ str.test = 5; // (*) alert(str.test); ``` -There may be two kinds of result: +这里有两种结果: 1. `undefined` -2. An error. +2. 报错。 -Why? Let's replay what's happening at line `(*)`: +在`(*)`的那一行到底发生了什么呢: -1. When a property of `str` is accessed, a "wrapper object" is created. -2. The operation with the property is carried out on it. So, the object gets the `test` property. -3. The operation finishes and the "wrapper object" disappears. +1. 当访问 `str` 属性时,创建一个“包裹对象”。 +2. 当对属性进行操作的时候。这个对象获得了 `test` 属性。 +3. 操作结束,“包裹对象”消失。 +在最后一行,对字符串上的新的包装对象的每个对象操作,`str` 不再追踪这个属性。 -So, on the last line, `str` has no trace of the property. A new wrapper object for every object operation on a string. +一些浏览器进一步限制程序员,并且不允许将属性分配给基本类型。 这就是为什么它有点远离规范,但在实践中我们却可以在`(*)`行看到错误。 -Some browsers though may decide to further limit the programmer and disallow to assign properties to primitives at all. That's why in practice we can also see errors at line `(*)`. It's a little bit farther from the specification though. +**这个例子清楚地表明,基本类型不是对象。** -**This example clearly shows that primitives are not objects.** +基本类型不能存储数据。 -They just can not store data. - -All property/method operations are performed with the help of temporary objects. +所有的属性/方法操作都是在临时对象的帮助下执行的。 From c5eff159bcb09756b7c3fe8ab00838a8168283a7 Mon Sep 17 00:00:00 2001 From: da Date: Tue, 1 May 2018 19:20:56 -0500 Subject: [PATCH 02/15] Update solution.md --- .../01-primitives-methods/1-string-new-property/solution.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md index e5faaed59d..e85d998afd 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md @@ -13,14 +13,14 @@ alert(str.test); 1. `undefined` 2. 报错。 -在`(*)`的那一行到底发生了什么呢: +在`(*)`的那一行到底发生了什么呢: 1. 当访问 `str` 属性时,创建一个“包裹对象”。 2. 当对属性进行操作的时候。这个对象获得了 `test` 属性。 3. 操作结束,“包裹对象”消失。 在最后一行,对字符串上的新的包装对象的每个对象操作,`str` 不再追踪这个属性。 -一些浏览器进一步限制程序员,并且不允许将属性分配给基本类型。 这就是为什么它有点远离规范,但在实践中我们却可以在`(*)`行看到错误。 +一些浏览器进一步限制程序员,并且不允许将属性分配给基本类型。这就是为什么它有点远离规范,但在实践中我们却可以在`(*)`行看到错误。 **这个例子清楚地表明,基本类型不是对象。** From 9141dd0eea2413eb9d7b683471f89403a56d2492 Mon Sep 17 00:00:00 2001 From: da Date: Tue, 1 May 2018 19:22:53 -0500 Subject: [PATCH 03/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index 50c781ea5d..ff18c5b76c 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -1,11 +1,11 @@ -importance: 5 +重点关注:5 --- -# Can I add a string property? +# 我能添加一个字符串属性吗? -Consider the following code: +思考下面代码: ```js let str = "Hello"; @@ -15,4 +15,4 @@ str.test = 5; alert(str.test); ``` -How do you think, will it work? What will be shown? +你怎么想呢?结果如何? From 86a2279f1cea677a580a04025a44ccbe24b5f6d0 Mon Sep 17 00:00:00 2001 From: da Date: Tue, 1 May 2018 19:44:27 -0500 Subject: [PATCH 04/15] Update article.md --- .../01-primitives-methods/article.md | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/article.md b/1-js/05-data-types/01-primitives-methods/article.md index e9075ef0b6..4b2dc84276 100644 --- a/1-js/05-data-types/01-primitives-methods/article.md +++ b/1-js/05-data-types/01-primitives-methods/article.md @@ -1,18 +1,18 @@ -# Methods of primitives +# 基本类型的方法 -JavaScript allows us to work with primitives (strings, numbers etc) as if they were objects. +JavaScript允许我们像对象一样使用基本类型(字符串,数字等)。 -They also provide methods to call and such. We will study those soon, but first we'll see how it works, because, of course, primitives are not objects (and here we will make it even more clear). +基本类型还提供调用方法等。我们会尽快研究这些,但首先我们会看看它是如何工作的,毕竟基本类型不是对象(在这里我们会更加清楚)。 -Let's look at the key distinction between primitives and objects. +我们来看看基本类型和对象之间的关键区别。 -A primitive +基本类型 -An object -: Is capable of storing multiple values as properties. -Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript, e.g. functions are objects. +对象 +:能够将多个值存储为属性。 +可以用`{}`创建,例如:`{name: "John", age: 30}`。JavaScript中还有其他种类的对象,例如函数就是对象。 -One of the best things about objects is that we can store a function as one of its properties: +关于对象的最好的事情之一是我们可以存储一个函数作为它的一个属性: ```js run let john = { @@ -25,15 +25,15 @@ let john = { john.sayHi(); // Hi buddy! ``` -So here we've made an object `john` with the method `sayHi`. +所以我们在这里用 `sayHi` 方法创建了一个对象 `john`。 -Many built-in objects already exist, such as those that work with dates, errors, HTML elements etc. They have different properties and methods. +许多内置对象已经存在,例如那些与日期,错误,HTML元素等一起工作的内置对象。它们具有不同的属性和方法。 -But, these features come with a cost! +但是,这些特性都是有成本的! -Objects are "heavier" than primitives. They require additional resources to support the internal machinery. But as properties and methods are very useful in programming, JavaScript engines try to optimize them to reduce the additional burden. +对象比基本对象“更重”。他们需要额外的资源来支持运作。但是,由于属性和方法在编程中非常有用,JavaScript引擎会尝试优化它们以减少额外的负担。 -## A primitive as an object +## 作为对象的基本类型 Here's the paradox faced by the creator of JavaScript: From 0a7ae483b2aa89fd489fb806a54400947611f5c6 Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 04:04:12 -0500 Subject: [PATCH 05/15] Update solution.md --- .../01-primitives-methods/1-string-new-property/solution.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md index e85d998afd..509d217209 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md @@ -15,9 +15,9 @@ alert(str.test); 在`(*)`的那一行到底发生了什么呢: -1. 当访问 `str` 属性时,创建一个“包裹对象”。 +1. 当访问 `str` 属性时,创建一个“包装对象”。 2. 当对属性进行操作的时候。这个对象获得了 `test` 属性。 -3. 操作结束,“包裹对象”消失。 +3. 操作结束,“包装对象”消失。 在最后一行,对字符串上的新的包装对象的每个对象操作,`str` 不再追踪这个属性。 一些浏览器进一步限制程序员,并且不允许将属性分配给基本类型。这就是为什么它有点远离规范,但在实践中我们却可以在`(*)`行看到错误。 From d0339a006d5e11459b2571378116a0730ec9876a Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 05:33:31 -0500 Subject: [PATCH 06/15] Update article.md --- .../01-primitives-methods/article.md | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/article.md b/1-js/05-data-types/01-primitives-methods/article.md index 4b2dc84276..04fa3448ac 100644 --- a/1-js/05-data-types/01-primitives-methods/article.md +++ b/1-js/05-data-types/01-primitives-methods/article.md @@ -35,22 +35,22 @@ john.sayHi(); // Hi buddy! ## 作为对象的基本类型 -Here's the paradox faced by the creator of JavaScript: +以下是 JavaScript 创建者面临的悖论: -- There are many things one would want to do with a primitive like a string or a number. It would be great to access them as methods. -- Primitives must be as fast and lightweight as possible. +- 有很多事情需要用像字符串或数字这样的基本类型来完成。这样要比直接访问他们方法要好。 +- 基本类型必须尽可能的精简快速。 -The solution looks a little bit awkward, but here it is: +该解决方案看起来有点尴尬,是: -1. Primitives are still primitive. A single value, as desired. -2. The language allows access to methods and properties of strings, numbers, booleans and symbols. -3. When this happens, a special "object wrapper" is created that provides the extra functionality, and then is destroyed. +1. 基本类型仍然是原始数据。根据需要提供单个值。 +2. JavaScript 允许访问字符串,数字,布尔值和符号的方法和属性。 +3. 当触发这种情况时,会创建一个特殊的“包装对象”,它提供额外的功能,运行后即被销毁。 -The "object wrappers" are different for each primitive type and are called: `String`, `Number`, `Boolean` and `Symbol`. Thus, they provide different sets of methods. +“包装对象”对于每种基本类型调用都是不同的,如`String`, `Number`, `Boolean` 和 `Symbol`。因此,他们提供了不同的方法。 -For instance, there exists a method [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) that returns a capitalized string. +例如,方法 [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase)返回一个大写的字符串。 -Here's how it works: +以下是它的工作原理: ```js run let str = "Hello"; @@ -58,17 +58,17 @@ let str = "Hello"; alert( str.toUpperCase() ); // HELLO ``` -Simple, right? Here's what actually happens in `str.toUpperCase()`: +很简单,对吧?以下是 `str.toUpperCase()` 实际发生的情况: -1. The string `str` is a primitive. So in the moment of accessing its property, a special object is created that knows the value of the string, and has useful methods, like `toUpperCase()`. -2. That method runs and returns a new string (shown by `alert`). -3. The special object is destroyed, leaving the primitive `str` alone. +1. 字符串 `str` 是一个基本类型。所以在访问它的属性时,会创建一个已知字符串值的特殊对象,并且具有有用的方法,例如 `toUpperCase()`。 +2. 该方法运行并返回一个新的字符串(由 `alert` 显示)。 +3. 特殊对象被破坏,只留下基本类型 `str`。 -So primitives can provide methods, but they still remain lightweight. +所以基本类型可以提供方法,但它们依然是轻量级的。 -The JavaScript engine highly optimizes this process. It may even skip the creation of the extra object at all. But it must still adhere to the specification and behave as if it creates one. +JavaScript引擎高度优化了这个过程。它甚至可能跳过创建额外的对象。但是它仍然必须遵守规范,并且表现得好像它创造了一样。 -A number has methods of its own, for instance, [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) rounds the number to the given precision: +数字有其自己的方法,例如,[toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)将数字四舍五入到给定的精度: ```js run let n = 1.23456; @@ -76,15 +76,15 @@ let n = 1.23456; alert( n.toFixed(2) ); // 1.23 ``` -We'll see more specific methods in chapters and . +我们将在后面章节中看到更具体的方法 。 ````warn header="Constructors `String/Number/Boolean` are for internal use only" -Some languages like Java allow us to create "wrapper objects" for primitives explicitly using a syntax like `new Number(1)` or `new Boolean(false)`. +像 Java 这样的一些语言允许我们使用 `new Number(1)` 或 `new Boolean(false)` 等语法明确地为基本类型创建“包装对象”。 -In JavaScript, that's also possible for historical reasons, but highly **unrecommended**. Things will go crazy in several places. +在 JavaScript 中,由于历史原因,这也是可以的,但高度**不推荐**。因为这样会出问题。 -For instance: +例如: ```js run alert( typeof 1 ); // "number" @@ -92,7 +92,7 @@ alert( typeof 1 ); // "number" alert( typeof new Number(1) ); // "object"! ``` -And because what follows, `zero`, is an object, the alert will show up: +因为接下来的是,`zero`,是一个对象,alert 将显示出来: ```js run let zero = new Number(0); @@ -102,9 +102,9 @@ if (zero) { // zero is true, because it's an object } ``` -On the other hand, using the same functions `String/Number/Boolean` without `new` is a totally sane and useful thing. They convert a value to the corresponding type: to a string, a number, or a boolean (primitive). +另一方面,不使用 `new` 的 `String/Number/Boolean` without `new` 是一个明智的选择。它们将一个值转换为相应的类型:转成 string,number,或 boolean(原始类型)。 -For example, this is entirely valid: +例如,下面完全是有效的: ```js let num = Number("123"); // convert a string to number ``` @@ -112,15 +112,15 @@ let num = Number("123"); // convert a string to number ````warn header="null/undefined have no methods" -The special primitives `null` and `undefined` are exceptions. They have no corresponding "wrapper objects" and provide no methods. In a sense, they are "the most primitive". +特殊的基本类型 `null` 和 `undefined` 是个例外。他们没有相应的“包装对象”,并没有提供任何方法。从某种意义上说,他们是“最原始的”。 -An attempt to access a property of such value would give the error: +尝试访问这种值的属性会导致错误: ```js run alert(null.test); // error ```` -## Summary +## 总结 -- Primitives except `null` and `undefined` provide many helpful methods. We will study those in the upcoming chapters. -- Formally, these methods work via temporary objects, but JavaScript engines are well tuned to optimize that internally, so they are not expensive to call. +- 除 `null` 和 `undefined` 以外的基本类型都提供了许多有用的方法。我们将在即将到来的章节中研究这些内容。 +- 从形式上讲,这些方法通过临时对象工作,但 JavaScript 引擎可以很好地调整以优化内部,因此调用它们运行成本并不昂贵。 From 71d80e8866fb0546347ae9c2b3ff28769010ec67 Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 06:30:58 -0500 Subject: [PATCH 07/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index ff18c5b76c..b63a0fdc4c 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -1,4 +1,4 @@ -重点关注:5 +重点性:5 --- From 0a3fd35539c2e7858d698fb3b4d0f3837a558209 Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 07:18:06 -0500 Subject: [PATCH 08/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index b63a0fdc4c..5ddc293455 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -1,4 +1,4 @@ -重点性:5 +重要性:5 --- From 49af46ed9d42e1b4f84398e8338d0153d5c43bc3 Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 07:21:21 -0500 Subject: [PATCH 09/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index 5ddc293455..50c781ea5d 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -1,11 +1,11 @@ -重要性:5 +importance: 5 --- -# 我能添加一个字符串属性吗? +# Can I add a string property? -思考下面代码: +Consider the following code: ```js let str = "Hello"; @@ -15,4 +15,4 @@ str.test = 5; alert(str.test); ``` -你怎么想呢?结果如何? +How do you think, will it work? What will be shown? From 9ebe5dfc9656a133662a8dafcfbcdc3327f7c762 Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 07:22:48 -0500 Subject: [PATCH 10/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index 50c781ea5d..19dd8bb3af 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -2,10 +2,10 @@ importance: 5 --- -# Can I add a string property? +# 我能添加一个字符串属性吗? -Consider the following code: +思考下面代码: ```js let str = "Hello"; @@ -15,4 +15,4 @@ str.test = 5; alert(str.test); ``` -How do you think, will it work? What will be shown? +你怎么想呢?结果如何? From 55e6f14ab42b1132f359b690545007003ffc173f Mon Sep 17 00:00:00 2001 From: da Date: Wed, 2 May 2018 18:36:16 -0500 Subject: [PATCH 11/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index 19dd8bb3af..50c781ea5d 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -2,10 +2,10 @@ importance: 5 --- -# 我能添加一个字符串属性吗? +# Can I add a string property? -思考下面代码: +Consider the following code: ```js let str = "Hello"; @@ -15,4 +15,4 @@ str.test = 5; alert(str.test); ``` -你怎么想呢?结果如何? +How do you think, will it work? What will be shown? From cea21ccc42844568269bea22fd6e91c0881fd6e3 Mon Sep 17 00:00:00 2001 From: da Date: Sun, 6 May 2018 19:00:30 -0500 Subject: [PATCH 12/15] Update solution.md --- .../01-primitives-methods/1-string-new-property/solution.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md index 509d217209..4569ab9344 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md @@ -13,14 +13,15 @@ alert(str.test); 1. `undefined` 2. 报错。 -在`(*)`的那一行到底发生了什么呢: +在 `(*)` 的那一行到底发生了什么呢: 1. 当访问 `str` 属性时,创建一个“包装对象”。 2. 当对属性进行操作的时候。这个对象获得了 `test` 属性。 3. 操作结束,“包装对象”消失。 + 在最后一行,对字符串上的新的包装对象的每个对象操作,`str` 不再追踪这个属性。 -一些浏览器进一步限制程序员,并且不允许将属性分配给基本类型。这就是为什么它有点远离规范,但在实践中我们却可以在`(*)`行看到错误。 +一些浏览器进一步限制程序员,并且不允许将属性分配给基本类型。这就是为什么它有点远离规范,但在实践中我们却可以在 `(*)` 行看到错误。 **这个例子清楚地表明,基本类型不是对象。** From b4955fb264aa6320263a51636d3ec5a31a887dab Mon Sep 17 00:00:00 2001 From: da Date: Sun, 6 May 2018 19:14:09 -0500 Subject: [PATCH 13/15] Update article.md --- .../01-primitives-methods/article.md | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/article.md b/1-js/05-data-types/01-primitives-methods/article.md index 04fa3448ac..435a0583c7 100644 --- a/1-js/05-data-types/01-primitives-methods/article.md +++ b/1-js/05-data-types/01-primitives-methods/article.md @@ -1,6 +1,6 @@ # 基本类型的方法 -JavaScript允许我们像对象一样使用基本类型(字符串,数字等)。 +JavaScript 允许我们像对象一样使用基本类型(字符串,数字等)。 基本类型还提供调用方法等。我们会尽快研究这些,但首先我们会看看它是如何工作的,毕竟基本类型不是对象(在这里我们会更加清楚)。 @@ -10,7 +10,7 @@ JavaScript允许我们像对象一样使用基本类型(字符串,数字等 对象 :能够将多个值存储为属性。 -可以用`{}`创建,例如:`{name: "John", age: 30}`。JavaScript中还有其他种类的对象,例如函数就是对象。 +可以用 `{}` 创建,例如:`{name: "John", age: 30}`。JavaScript 中还有其他种类的对象,例如函数就是对象。 关于对象的最好的事情之一是我们可以存储一个函数作为它的一个属性: @@ -25,13 +25,13 @@ let john = { john.sayHi(); // Hi buddy! ``` -所以我们在这里用 `sayHi` 方法创建了一个对象 `john`。 +所以我们在这里创建了一个包含 `sayHi` 方法的对象 `john`。 -许多内置对象已经存在,例如那些与日期,错误,HTML元素等一起工作的内置对象。它们具有不同的属性和方法。 +许多内置对象已经存在,例如那些与日期,错误,HTML 元素等一起工作的内置对象。它们具有不同的属性和方法。 但是,这些特性都是有成本的! -对象比基本对象“更重”。他们需要额外的资源来支持运作。但是,由于属性和方法在编程中非常有用,JavaScript引擎会尝试优化它们以减少额外的负担。 +对象比基本对象“更重”。他们需要额外的资源来支持运作。但是,由于属性和方法在编程中非常有用,JavaScript 引擎会尝试优化它们以减少额外的负担。 ## 作为对象的基本类型 @@ -48,7 +48,7 @@ john.sayHi(); // Hi buddy! “包装对象”对于每种基本类型调用都是不同的,如`String`, `Number`, `Boolean` 和 `Symbol`。因此,他们提供了不同的方法。 -例如,方法 [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase)返回一个大写的字符串。 +例如,方法 [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) 返回一个大写的字符串。 以下是它的工作原理: @@ -62,13 +62,13 @@ alert( str.toUpperCase() ); // HELLO 1. 字符串 `str` 是一个基本类型。所以在访问它的属性时,会创建一个已知字符串值的特殊对象,并且具有有用的方法,例如 `toUpperCase()`。 2. 该方法运行并返回一个新的字符串(由 `alert` 显示)。 -3. 特殊对象被破坏,只留下基本类型 `str`。 +3. 特殊对象被销毁,只留下基本类型 `str`。 所以基本类型可以提供方法,但它们依然是轻量级的。 -JavaScript引擎高度优化了这个过程。它甚至可能跳过创建额外的对象。但是它仍然必须遵守规范,并且表现得好像它创造了一样。 +JavaScript 引擎高度优化了这个过程。它甚至可能跳过创建额外的对象。但是它仍然必须遵守规范,并且表现得好像它创造了一样。 -数字有其自己的方法,例如,[toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)将数字四舍五入到给定的精度: +数字有其自己的方法,例如,[toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) 将数字四舍五入到给定的精度: ```js run let n = 1.23456; @@ -82,7 +82,7 @@ alert( n.toFixed(2) ); // 1.23 ````warn header="Constructors `String/Number/Boolean` are for internal use only" 像 Java 这样的一些语言允许我们使用 `new Number(1)` 或 `new Boolean(false)` 等语法明确地为基本类型创建“包装对象”。 -在 JavaScript 中,由于历史原因,这也是可以的,但高度**不推荐**。因为这样会出问题。 +在 JavaScript 中,由于历史原因,这也是可以的,但极其**不推荐**。因为这样会出问题。 例如: @@ -92,7 +92,7 @@ alert( typeof 1 ); // "number" alert( typeof new Number(1) ); // "object"! ``` -因为接下来的是,`zero`,是一个对象,alert 将显示出来: +同样的,`zero`,是一个对象,alert 将显示出来: ```js run let zero = new Number(0); @@ -102,7 +102,7 @@ if (zero) { // zero is true, because it's an object } ``` -另一方面,不使用 `new` 的 `String/Number/Boolean` without `new` 是一个明智的选择。它们将一个值转换为相应的类型:转成 string,number,或 boolean(原始类型)。 +另一方面,不使用 `new` 的 `String/Number/Boolean` 是一个明智的选择。它们将一个值转换为相应的类型:转成 string,number,或 boolean(原始类型)。 例如,下面完全是有效的: ```js From 89a634a9ba4dbad4d4e19d5d1a3555313ade6b19 Mon Sep 17 00:00:00 2001 From: da Date: Sun, 6 May 2018 21:09:35 -0500 Subject: [PATCH 14/15] Update article.md --- 1-js/05-data-types/01-primitives-methods/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/05-data-types/01-primitives-methods/article.md b/1-js/05-data-types/01-primitives-methods/article.md index 435a0583c7..6aebd6dd81 100644 --- a/1-js/05-data-types/01-primitives-methods/article.md +++ b/1-js/05-data-types/01-primitives-methods/article.md @@ -9,7 +9,7 @@ JavaScript 允许我们像对象一样使用基本类型(字符串,数字等 基本类型 对象 -:能够将多个值存储为属性。 +:能够将多个值存储为属性。 可以用 `{}` 创建,例如:`{name: "John", age: 30}`。JavaScript 中还有其他种类的对象,例如函数就是对象。 关于对象的最好的事情之一是我们可以存储一个函数作为它的一个属性: From f72fc0d08b691787a464b83f3497d8d19812ed33 Mon Sep 17 00:00:00 2001 From: da Date: Sun, 6 May 2018 21:46:14 -0500 Subject: [PATCH 15/15] Update task.md --- .../01-primitives-methods/1-string-new-property/task.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md index 50c781ea5d..19dd8bb3af 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md @@ -2,10 +2,10 @@ importance: 5 --- -# Can I add a string property? +# 我能添加一个字符串属性吗? -Consider the following code: +思考下面代码: ```js let str = "Hello"; @@ -15,4 +15,4 @@ str.test = 5; alert(str.test); ``` -How do you think, will it work? What will be shown? +你怎么想呢?结果如何?