Skip to content

Commit 7b6fb69

Browse files
author
howardchi
committed
finished core/delete.md, types/casting.md, equality.md, instanceof.md
1 parent 63388c6 commit 7b6fb69

File tree

4 files changed

+57
-80
lines changed

4 files changed

+57
-80
lines changed

doc/zh-TW/core/delete.md

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
## The `delete` Operator
1+
## `delete` 控制符
22

3-
In short, it's *impossible* to delete global variables, functions and some other
4-
stuff in JavaScript which have a `DontDelete` attribute set.
3+
簡單來說,那是 *不可能* 去刪除一個全域變數,函式和其他東西在 JavaScript 中有一個 `DontDelete` 的屬性
54

6-
### Global code and Function code
5+
### 全域和函式
76

8-
When a variable or a function is defined in a global or a [function
9-
scope](#function.scopes) it is a property of either the Activation object or
10-
the Global object. Such properties have a set of attributes, one of which is
11-
`DontDelete`. Variable and function declarations in global and function code
12-
always create properties with `DontDelete`, and therefore cannot be deleted.
7+
當一個變數或是一個函式在一個全域範圍被定義或是在一個 [funciton scope](#function.scopes) ,這些屬性可能是動態的物件或是全域的物件。這些特性有一系列的屬性。其中一個就是 `DontDelete`
8+
在這些變數和函式的宣告都會有一個屬性叫 `DontDelete`,這會使得它無法被刪除。
139

14-
// global variable:
15-
var a = 1; // DontDelete is set
10+
// 全域變數
11+
var a = 1; // DontDelete 屬性被建立
1612
delete a; // false
1713
a; // 1
1814

1915
// normal function:
20-
function f() {} // DontDelete is set
16+
function f() {} // DontDelete 屬性被建立
2117
delete f; // false
2218
typeof f; // "function"
2319

@@ -26,9 +22,9 @@ always create properties with `DontDelete`, and therefore cannot be deleted.
2622
delete f; // false
2723
f; // 1
2824

29-
### Explicit properties
25+
### 明確的屬性
3026

31-
Explicitly set properties can be deleted normally.
27+
明確的屬性可以被簡單的刪除。
3228

3329
// explicitly set property:
3430
var obj = {x: 1};
@@ -38,28 +34,25 @@ Explicitly set properties can be deleted normally.
3834
obj.x; // undefined
3935
obj.y; // undefined
4036

41-
In the example above, `obj.x` and `obj.y` can be deleted because they have no
42-
`DontDelete` atribute. That's why the example below works too.
37+
在上面的例子中, `obj.x` `obj.y` 可以被刪除是因為他們沒有 `DontDelete` 的屬性。
38+
所以下面的例子也可以這樣用。
4339

44-
// this works fine, except for IE:
40+
// 可以運作,除了 IE:
4541
var GLOBAL_OBJECT = this;
4642
GLOBAL_OBJECT.a = 1;
4743
a === GLOBAL_OBJECT.a; // true - just a global var
4844
delete GLOBAL_OBJECT.a; // true
4945
GLOBAL_OBJECT.a; // undefined
5046

51-
Here we use a trick to delete `a`. [`this`](#function.this) here refers
52-
to the Global object and we explicitly declare variable `a` as its property
53-
which allows us to delete it.
47+
這裡我們想要去刪除 `a`[`this`](#funciton.this) 這裡指向一個全域的物件,和我們明確了地定義 `a` 是它的屬性,所以可以刪除它。
5448

55-
IE (at least 6-8) has some bugs, so the code above doesn't work.
49+
IE 有些臭蟲,所以上面的程式碼無法使用(至少 6~8)
5650

57-
### Function arguments and built-ins
51+
### 函式的參數和內建
5852

59-
Functions' normal arguments, [`arguments` objects](#function.arguments)
60-
and built-in properties also have `DontDelete` set.
53+
函式的普通參數,[`arguments` object](#function.arguments) 還有一些內建的屬性都有 `DontDelete` 的建立
6154

62-
// function arguments and properties:
55+
// function 參數和屬性
6356
(function (x) {
6457

6558
delete arguments; // false
@@ -74,12 +67,12 @@ and built-in properties also have `DontDelete` set.
7467
7568
})(1);
7669

77-
### Host objects
78-
79-
The behaviour of `delete` operator can be unpredictable for hosted objects. Due
80-
to the specification, host objects are allowed to implement any kind of behavior.
70+
### 接受物件
71+
72+
控制符可以接受無法預測的物件。由於一些特別的情況,會允許它能夠 `delete`
73+
74+
### 結語
75+
76+
`delete` 控制符通常都有難以預料的行為,所以我們只可以安全的刪除顯著的屬性在普通的物件上。
8177

82-
### In conclusion
8378

84-
The `delete` operator often has unexpected behaviour and can only be safely
85-
used to delete explicitly set properties on normal objects.

doc/zh-TW/types/casting.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
## Type Casting
1+
## 類型轉換
22

3-
JavaScript is a *weakly typed* language, so it will apply *type coercion*
4-
**wherever** possible.
3+
JavaScript 是一個 *弱類型* 的程式語言,所以在 **任何** 情況下都可以 *強制類型轉換*
54

6-
// These are true
5+
// 這些都是真
76
new Number(10) == 10; // Number.toString() is converted
87
// back to a number
98

@@ -13,13 +12,15 @@ JavaScript is a *weakly typed* language, so it will apply *type coercion*
1312
isNaN(null) == false; // null converts to 0
1413
// which of course is not NaN
1514

16-
// These are false
15+
// 下面都假
1716
10 == 010;
1817
10 == '-10';
1918

2019
> **ES5 Note:** Number literals that start with a `0` are interpreted as octal
2120
> (Base 8). Octal support for these has been **removed** in ECMAScript 5 strict
2221
> mode.
22+
>
23+
2324

2425
To avoid the issues above, use of the [strict equal operator](#types.equality)
2526
is **highly** recommended. Although this avoids a lot of common pitfalls, there

doc/zh-TW/types/equality.md

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
## Equality and Comparisons
1+
## 相等與比較
22

3-
JavaScript has two different ways of comparing the values of objects for equality.
3+
JavaScript 有兩個不同的方式來比較兩個物件是否相等。
44

5-
### The Equality Operator
5+
### 等於操作符
66

7-
The equality operator consists of two equal signs: `==`
7+
等於操作符是由兩個等號組成: `==`
88

9-
JavaScript features *weak typing*. This means that the equality operator
10-
**coerces** types in order to compare them.
9+
JavaScript 是一個 *弱類型* 語言。這代表它會為了比較兩個值而做 **強制類型轉換**
1110

1211
"" == "0" // false
1312
0 == "" // true
@@ -19,20 +18,14 @@ JavaScript features *weak typing*. This means that the equality operator
1918
null == undefined // true
2019
" \t\r\n" == 0 // true
2120

22-
The above table shows the results of the type coercion, and it is the main reason
23-
why the use of `==` is widely regarded as bad practice. It introduces
24-
hard-to-track-down bugs due to its complicated conversion rules.
21+
上面的表格可以看出來這些結果強制轉換類型,這也代表說用 `==` 是一個不好的習慣,因為它會很難追蹤問題由於它複雜的規則。
2522

26-
Additionally, there is also a performance impact when type coercion is in play;
27-
for example, a string has to be converted to a number before it can be compared
28-
to another number.
23+
此外,也有效率上面的問題在強制轉換類型。
24+
例如說一個字串會被轉成數字來和別的數字做比較。
2925

30-
### The Strict Equality Operator
26+
### 嚴格等於操作符
3127

32-
The strict equality operator consists of **three** equal signs: `===`.
33-
34-
It works like the normal equality operator, except that strict equality
35-
operator does **not** perform type coercion between its operands.
28+
不像普通的等於操作符 `===` 不會做強制類型轉換。
3629

3730
"" === "0" // false
3831
0 === "" // false
@@ -44,28 +37,24 @@ operator does **not** perform type coercion between its operands.
4437
null === undefined // false
4538
" \t\r\n" === 0 // false
4639

47-
The above results are a lot clearer and allow for early breakage of code. This
48-
hardens code to a certain degree and also gives performance improvements in case
49-
the operands are of different types.
40+
上面的結果比較清楚,也有利於程式碼的分析。如果這兩個操作數的類型不一樣都就不會相等,有助於它性能的提昇。
5041

51-
### Comparing Objects
42+
### 比較物件
5243

53-
While both `==` and `===` are called **equality** operators, they behave
54-
differently when at least one of their operands is an `Object`.
44+
雖然 `==``===` 都是等於操作符,但其中有一個操作數為物件時,它的行為就會不同。
5545

5646
{} === {}; // false
5747
new String('foo') === 'foo'; // false
5848
new Number(10) === 10; // false
5949
var foo = {};
6050
foo === foo; // true
6151

62-
Here, both operators compare for **identity** and **not** equality; that is, they
63-
will compare for the same **instance** of the object, much like `is` in Python
64-
and pointer comparison in C.
52+
在這裡等於操作符比較 **不是** 值的相等,而是否是 **相同** 的身分。
53+
有點像 Python 的 `is` 和 C 中的指標。
54+
55+
### 結論
6556

66-
### In Conclusion
57+
強烈建議使用 **嚴格等於**
58+
如果要轉換類型,應該要在 [explicitly](#types.casting)的時候轉換,而不是在語言本身用複雜的轉換規則。
6759

68-
It is highly recommended to only use the **strict equality** operator. In cases
69-
where types need to be coerced, it should be done [explicitly](#types.casting)
70-
and not left to the language's complicated coercion rules.
7160

doc/zh-TW/types/instanceof.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
## The `instanceof` Operator
1+
## `instanceof` 操作符
22

3-
The `instanceof` operator compares the constructors of its two operands. It is
4-
only useful when comparing custom made objects. Used on built-in types, it is
5-
nearly as useless as the [typeof operator](#types.typeof).
3+
`instanceof` 操作符用來比較兩個建構函數的操作數。只有在比較字定義的物件時才有意義。這和 [typeof operator](#types.typeof)一樣用處不大。
64

7-
### Comparing Custom Objects
5+
### 比較定意義物件
86

97
function Foo() {}
108
function Bar() {}
@@ -18,21 +16,17 @@ nearly as useless as the [typeof operator](#types.typeof).
1816
Bar.prototype = Foo;
1917
new Bar() instanceof Foo; // false
2018

21-
### Using `instanceof` with Native Types
19+
### `instanceof` 比較內置類型
2220

2321
new String('foo') instanceof String; // true
2422
new String('foo') instanceof Object; // true
2523

2624
'foo' instanceof String; // false
2725
'foo' instanceof Object; // false
2826

29-
One important thing to note here is that `instanceof` does not work on objects
30-
that originate from different JavaScript contexts (e.g. different documents
31-
in a web browser), since their constructors will not be the exact same object.
27+
有一點需要注意的, `instanceof` 不能用來物件來自上下文不同的屬性(例如:瀏覽器中不同的文檔結構),因為它的建構函數不一樣。
3228

3329
### In Conclusion
3430

35-
The `instanceof` operator should **only** be used when dealing with custom made
36-
objects that originate from the same JavaScript context. Just like the
37-
[`typeof`](#types.typeof) operator, every other use of it should be **avoided**.
38-
31+
`instanceof` 操作符應該 **** 用來比較同一個 JavaScript 上下文定意義的物件。
32+
正如 [`typeof`](#types.typeof)操作符一樣,任何其他用法都要避免。

0 commit comments

Comments
 (0)