Skip to content

Commit 821a9b5

Browse files
committed
Add equality array constructor sections
1 parent 6320fef commit 821a9b5

21 files changed

+2082
-224
lines changed

doc/language.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"default": "en",
2+
"default": "zh",
33
"listed": ["en", "zh"]
44
}

doc/zh/array/constructor.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
## The `Array` Constructor
1+
## `Array` 构造函数
22

3-
Since the `Array` constructor is ambiguous in how it deals with its parameters,
4-
it is highly recommended to always use the array literals - `[]` notation -
5-
when creating new arrays.
3+
由于 `Array` 的构造函数在如何处理参数时有点模棱两可,因此总是推荐使用数组的字面语法 - `[]` - 来创建数组。
64

7-
[1, 2, 3]; // Result: [1, 2, 3]
8-
new Array(1, 2, 3); // Result: [1, 2, 3]
5+
[1, 2, 3]; // 结果: [1, 2, 3]
6+
new Array(1, 2, 3); // 结果: [1, 2, 3]
97

10-
[3]; // Result: [3]
11-
new Array(3); // Result: []
12-
new Array('3') // Result: ['3']
8+
[3]; // 结果: [3]
9+
new Array(3); // 结果: []
10+
new Array('3') // 结果: ['3']
1311

14-
In cases when there is only one argument passed to the `Array` constructor,
15-
and that argument is a `Number`, the constructor will return a new *sparse*
16-
array with the `length` property set to the value of the argument. It should be
17-
noted that **only** the `length` property of the new array will be set this way,
18-
the actual indexes of the array will not be initialized.
12+
译者注:这里的模棱两可指的是数组的[两种构造函数语法][1]
13+
var arr1 = new Array(arrayLength);
14+
var arr2 = new Array(element0, element1, ..., elementN);
15+
16+
// 译者注:因此下面的代码将会使人很迷惑
17+
new Array(3, 4, 5); // 结果: [3, 4, 5]
18+
new Array(3) // 结果: [],此数组长度为 3
19+
20+
由于只有一个参数传递到构造函数中(译者注:指的是 `new Array(3);` 这种调用方式),并且这个参数是数字,构造函数会返回一个 `length` 属性被设置为此参数的空数组。
21+
需要特别注意的是,此时只有 `length` 属性被设置,真正的数组并没有生成。
22+
译者注:在 Firebug 中,你会看到 [undefined, undefined, undefined],这其实是不对的。在上一节有详细的分析。
1923

2024
var arr = new Array(3);
2125
arr[1]; // undefined
22-
1 in arr; // false, the index was not set
26+
1 in arr; // false, 数组还没有生成
2327

24-
The behavior of being able to set the length of the array upfront only comes in
25-
handy in a few cases, like repeating a string, in which it avoids the use of a
26-
`for loop` code.
28+
这种优先于设置数组长度属性的做法只在少数几种情况下有用,比如需要循环字符串,可以避免 `for` 循环的麻烦。
2729

2830
new Array(count + 1).join(stringToRepeat);
31+
// 译者注:new Array(3).join('#') 将会返回 "##"
2932

30-
### In conclusion
33+
### 结论(In conclusion
3134

32-
The use of the `Array` constructor should be avoided as much as possible.
33-
Literals are definitely preferred. They are shorter and have a clearer syntax;
34-
therefore, they also increase the readability of the code.
35+
应该尽量避免使用数组构造函数创建新数组。推荐使用数组的字面语法。它们更加短小和简洁,因此增加了代码的可读性。
36+
37+
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
3538

doc/zh/array/general.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
1-
## Array Iteration and Properties
1+
## 数组遍历与属性
22

3-
Although arrays in JavaScript are objects, there are no good reasons to use
4-
the [`for in loop`](#object.forinloop) in for iteration on them. In fact there
5-
are a number of good reasons **against** the use of `for in` on arrays.
3+
虽然在 JavaScript 中数组是是对象,但是没有好的理由去使用 [`for in` 循环](#object.forinloop) 遍历数组。
4+
相反,有一些好的理由**不去**使用 `for in` 遍历数组。
65

7-
> **Note:** JavaScript arrays are **not** *associative arrays*. JavaScript only
8-
> has [objects](#object.general) for mapping keys to values. And while associative
9-
> arrays **preserve** order, objects **do not**.
6+
> **注意:** JavaScript 中数组**不是** *关联数组*
7+
> JavaScript 中只有[对象](#object.general) 来管理键值的对应关系。但是关联数组是**保持**顺序的,而对象**不是**
108
11-
Since the `for in` loop enumerates all the properties that are on the prototype
12-
chain and the only way to exclude those properties is to use
13-
[`hasOwnProperty`](#object.hasownproperty), it is already up to **twenty times**
14-
slower than a normal `for` loop.
9+
由于 `for in` 循环会枚举原型链上的所有属性,唯一过滤这些属性的方式是使用 [`hasOwnProperty`](#object.hasownproperty) 函数,
10+
因此会比普通的 `for` 循环慢上好多倍。
1511

16-
### Iteration
12+
### 遍历(Iteration
1713

18-
In order to achieve the best performance when iterating over arrays, it is best
19-
to use the classic `for` loop.
14+
为了达到遍历数组的最佳性能,推荐使用经典的 `for` 循环。
2015

2116
var list = [1, 2, 3, 4, 5, ...... 100000000];
2217
for(var i = 0, l = list.length; i < l; i++) {
2318
console.log(list[i]);
2419
}
2520

26-
There is one extra catch in the above example, that is the caching of the
27-
length of the array via `l = list.length`.
21+
上面代码有一个处理,就是通过 `l = list.length` 来缓存数组的长度。
2822

29-
Although the `length` property is defined on the array itself, there is still an
30-
overhead for doing the lookup on each iteration of the loop. And while recent
31-
JavaScript engines **may** apply optimization in this case, there is no way of
32-
telling whether the code will run on one of these newer engines or not.
23+
虽然 `length` 是数组的一个属性,但是在每次循环中访问它还是有性能开销。
24+
**可能**最新的 JavaScript 引擎在这点上做了优化,但是我们没法保证自己的代码是否运行在这些最近的引擎之上。
3325

34-
In fact, leaving out the caching may result in the loop being only **half as
35-
fast** as with the cached length.
26+
实际上,不使用缓存数组长度的方式比缓存版本要慢很多。
3627

37-
### The `length` property
3828

39-
While the *getter* of the `length` property simply returns the number of
40-
elements that are contained in the array, the *setter* can be used to
41-
**truncate** the array.
29+
### `length` 属性(The `length` property
30+
31+
`length` 属性的 *getter* 方式会简单的返回数组的长度,而 *setter* 方式会**截断**数组。
4232

4333
var foo = [1, 2, 3, 4, 5, 6];
4434
foo.length = 3;
@@ -47,12 +37,21 @@ elements that are contained in the array, the *setter* can be used to
4737
foo.length = 6;
4838
foo; // [1, 2, 3]
4939

50-
Assigning a smaller length does truncate the array, but increasing the length
51-
does not have any effect on the array.
40+
译者注:
41+
在 Firebug 中查看此时 foo 的值是: [1, 2, 3, undefined, undefined, undefined]
42+
但是这个结果并不准确,如果你在 Chrome 的控制台查看 foo 的结果,你会发现是这样的: [1, 2, 3]
43+
因为在 JavaScript 中 undefined 是一个变量,注意是变量不是关键字,因此上面两个结果的意义是完全不相同的。
44+
45+
// 译者注:为了验证,我们来执行下面代码,看序号 5 是否存在于 foo 中。
46+
5 in foo; // 不管在 Firebug 或者 Chrome 都返回 false
47+
foo[5] = undefined;
48+
5 in foo; // 不管在 Firebug 或者 Chrome 都返回 true
49+
50+
51+
`length` 设置一个更小的值会截断数组,但是增大 `length` 属性值不会对数组产生影响。
5252

53-
### In conclusion
53+
### 结论(In conclusion
5454

55-
For the best performance it is recommended to always use the plain `for` loop
56-
and cache the `length` property. The use of `for in` on an array is a sign of
57-
badly written code that is prone to bugs and bad performance.
55+
为了更好的性能,推荐使用普通的 `for` 循环并缓存数组的 `length` 属性。
56+
使用 `for in` 遍历数组被认为是不好的代码习惯并倾向于产生错误和导致性能问题。
5857

doc/zh/function/arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## `arguments` 对象(The `arguments` object)
1+
## `arguments` 对象
22

33
JavaScript 中每个函数内都能访问一个特别变量 `arguments`。这个变量维护着所有传递到这个函数中的参数列表。
44

doc/zh/function/closures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 闭包和引用(Closures and references)
1+
## 闭包和引用
22

33
闭包是 JavaScript 一个非常重要的特性,这意味着当前作用域**总是**能够访问外部作用域中的变量。
44
因为 [函数](#scopes) 是 JavaScript 中唯一拥有自身作用域的结构,因此闭包的创建依赖于函数。

doc/zh/function/constructors.md

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
## Constructors
1+
## 构造函数
22

3-
Constructors in JavaScript are yet again different from many other languages. Any
4-
function call that is preceded by the `new` keyword acts as a constructor.
3+
JavaScript 中的构造函数和其它语言中的构造函数是不同的。
4+
通过 `new` 关键字方式调用的函数都被认为是构造函数。
55

6-
Inside the constructor - the called function - the value of `this` refers to a
7-
newly created `Object`. The [`prototype`](#object.prototype) of this **new**
8-
object is set to the `prototype` of the function object that was invoked as the
9-
constructor.
6+
在构造函数内部 - 也就是被调用的函数内 - `this` 指向新创建的对象 `Object`
7+
这个**新创建**的对象的 [`prototype`](#object.prototype) 被指向到构造函数的 `prototype`
108

11-
If the function that was called has no explicit `return` statement, then it
12-
implicitly returns the value of `this` - the new object.
9+
如果被调用的函数没有显式的 `return` 表达式,则隐式的会返回 `this` 对象 - 也就是新创建的对象。
1310

1411
function Foo() {
1512
this.bla = 1;
@@ -21,41 +18,48 @@ implicitly returns the value of `this` - the new object.
2118

2219
var test = new Foo();
2320

24-
The above calls `Foo` as constructor and sets the `prototype` of the newly
25-
created object to `Foo.prototype`.
21+
上面代码把 `Foo` 作为构造函数调用,并设置新创建对象的 `prototype``Foo.prototype`
2622

27-
In case of an explicit `return` statement the function returns the value
28-
specified that statement, **but only** if the return value is an `Object`.
23+
显式的 `return` 表达式将会影响返回结果,但**仅限**于返回的是一个对象。
2924

3025
function Bar() {
3126
return 2;
3227
}
33-
new Bar(); // a new object
34-
28+
new Bar(); // 返回新创建的对象
29+
30+
// 译者注:new Bar() 返回的是新创建的对象,而不是数字的字面值 2。
31+
// 因此 new Bar().constructor === Bar
32+
// 但是如果返回的是数字对象,结果就不同了
33+
// function Bar() {
34+
// return new Number(2);
35+
// }
36+
// new Bar().constructor === Number
37+
38+
3539
function Test() {
3640
this.value = 2;
3741

3842
return {
3943
foo: 1
4044
};
4145
}
42-
new Test(); // the returned object
46+
new Test(); // 返回的对象
47+
// 译者注:这里得到的是函数返回的对象,而不是通过 new 关键字新创建的对象
48+
// 所有 (new Test()).value 为 undefined,但是 (new Test()).foo === 1。
4349

44-
When the `new` keyword is omitted, the function will **not** return a new object.
50+
如果 `new` 被遗漏了,则函数**不会**返回新创建的对象。
4551

4652
function Foo() {
47-
this.bla = 1; // gets set on the global object
53+
this.bla = 1; // 获取设置全局参数
4854
}
4955
Foo(); // undefined
5056

51-
While the above example might still appear to work in some cases, due to the
52-
workings of [`this`](#function.this) in JavaScript, it will use the
53-
*global object* as the value of `this`.
57+
虽然上例在有些情况下也能正常运行,但是由于 JavaScript 中 [`this`](#function.this) 的工作原理,
58+
这里的 `this` 指向*全局对象*
5459

55-
### Factories
60+
### 工厂模式(Factories
5661

57-
In order to be able to omit the `new` keyword, the constructor function has to
58-
explicitly return a value.
62+
为了不使用 `new` 关键字,构造函数必须显式的返回一个值。
5963

6064
function Bar() {
6165
var value = 1;
@@ -72,25 +76,30 @@ explicitly return a value.
7276
new Bar();
7377
Bar();
7478

75-
Both calls to `Bar` return the exact same thing, a newly create object which
76-
has a property called `method` on it, that is a
77-
[Closure](#function.closures).
79+
上面两种对 `Bar` 函数的调用返回的值完全相同,一个新创建的拥有 `method` 属性的对象被返回,
80+
其实这里创建了一个[闭包](#function.closures)
81+
7882

79-
It is also to note that the call `new Bar()` does **not** affect the prototype
80-
of the returned object. While the prototype will be set on the newly created
81-
object, `Bar` never returns that new object.
83+
还需要注意,`new Bar()`**不会**改变返回对象的原型(译者注:也就是返回对象的原型不会指向 Bar.prototype)。
84+
因为构造函数的原型会被指向到刚刚创建的新对象,而这里的 `Bar` 没有把这个新对象返回(译者注:而是返回了一个包含 `method` 属性的自定义对象)。
8285

83-
In the above example, there is no functional difference between using and
84-
not using the `new` keyword.
86+
在上面的例子中,使用或者不使用 `new` 关键字没有功能性的区别。
8587

88+
// 译者注:上面两种方式创建的对象不能访问 Bar 原型链上的属性
89+
var bar1 = new Bar();
90+
typeof(bar1.method); // "function"
91+
typeof(bar1.foo); // "undefined"
92+
93+
var bar2 = Bar();
94+
typeof(bar2.method); // "function"
95+
typeof(bar2.foo); // "undefined"
8696

87-
### Creating new objects via factories
8897

89-
An often made recommendation is to **not** use `new` since forgetting its use
90-
may lead to bugs.
98+
### 通过工厂模式创建新对象(Creating new objects via factories)
9199

92-
In order to create new object, one should rather use a factory and construct a
93-
new object inside of that factory.
100+
我们常听到的一条忠告是**不要**使用 `new` 关键字来调用函数,因为如果忘记使用它就会导致错误。
101+
102+
为了创建新对象,我们可以创建一个工厂方法,并且在方法内构造一个新对象。
94103

95104
function Foo() {
96105
var obj = {};
@@ -107,22 +116,16 @@ new object inside of that factory.
107116
return obj;
108117
}
109118

110-
While the above is robust against a missing `new` keyword and certainly makes
111-
the use of [private variables](#function.closures) easier, it comes with some
112-
downsides.
119+
虽然上面的方式比起 `new` 的调用方式不容易出错,并且可以充分利用[私有变量](#function.closures)带来的便利,
120+
但是随之而来的是一些不好的地方。
121+
113122

114-
1. It uses more memory since the created objects do **not** share the methods
115-
on a prototype.
116-
2. In order to inherit the factory needs to copy all the methods from another
117-
object or put that object on the prototype of the new object.
118-
3. Dropping the prototype chain just because of a left out `new` keyword
119-
somehow goes against the spirit of the language.
123+
1. 会占用更多的内存,因为新创建的对象**不能**共享原型上的方法。
124+
2. 为了实现继承,工厂方法需要从另外一个对象拷贝所有属性,或者把一个对象作为新创建对象的原型。
125+
3. 放弃原型链仅仅是因为防止遗漏 `new` 带来的问题,这似乎和语言本身的思想相违背。
120126

121-
### In conclusion
127+
### 总结(In conclusion
122128

123-
While omitting the `new` keyword might lead to bugs, it is certainly **not** a
124-
reason to drop the use of prototypes altogether. In the end it comes down to
125-
which solution is better suited for the needs of the application, it is
126-
especially important to choose a specific style of object creation **and stick**
127-
with it.
129+
虽然遗漏 `new` 关键字可能会导致问题,但这并**不是**放弃使用原型链的借口。
130+
最终使用哪种方式取决于应用程序的需求,选择一种代码书写风格并**坚持**下去才是最重要的。
128131

doc/zh/function/general.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 函数声明与表达式(Function Declarations and Expressions)
1+
## 函数声明与表达式
22

33
函数是JavaScript中的一等对象,这意味着可以把函数像其它值一样传递。
44
一个常见的用法是把*匿名函数*作为回调函数传递对异步函数中。

doc/zh/function/scopes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 作用域与命名空间(Scopes and namespaces)
1+
## 作用域与命名空间
22

33
尽管 JavaScript 支持一对花括号创建的代码段,但是并不支持块级作用域;
44
而仅仅支持 *函数作用域*

doc/zh/function/this.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## this 的工作原理(How `this` works)
1+
## this 的工作原理
22

33
JavaScript 有一套完全不同于其它语言的对 `this` 的处理机制。
44
****种不同的情况下 ,`this` 指向的各不相同。

0 commit comments

Comments
 (0)