Skip to content

Commit 2143895

Browse files
committed
Add instanceOf and casting section
1 parent 70feb5e commit 2143895

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

doc/zh/types/casting.md

+43-36
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,70 @@
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
7-
new Number(10) == 10; // Number.toString() is converted
8-
// back to a number
5+
// 下面的比较结果是:true
6+
new Number(10) == 10; // Number.toString() 返回的字符串被再次转换为数字
97

10-
10 == '10'; // Strings gets converted to Number
11-
10 == '+10 '; // More string madness
12-
10 == '010'; // And more
13-
isNaN(null) == false; // null converts to 0
14-
// which of course is not NaN
8+
10 == '10'; // 字符串被转换为数字
9+
10 == '+10 '; // 同上
10+
10 == '010'; // 同上
11+
isNaN(null) == false; // null 被转换为数字 0
12+
// 0 当然不是一个 NaN(译者注:否定之否定)
1513

16-
// These are false
14+
// 下面的比较结果是:false
1715
10 == 010;
1816
10 == '-10';
1917

20-
> **ES5 Note:** Number literals that start with a `0` are interpreted as octal
21-
> (Base 8). Octal support for these has been **removed** in ECMAScript 5 strict
22-
> mode.
18+
> **ES5 提示:**`0` 开头的数字字面值会被作为八进制数字解析。
19+
> 而在 ECMAScript 5 严格模式下,这个特性被**移除**了。
2320
24-
In order to avoid the above, use of the [strict equal operator](#types.equality)
25-
is **highly** recommended. Although this avoids a lot of common pitfalls, there
26-
are still many further issues that arise from JavaScript's weak typing system.
21+
为了避免上面复杂的强制类型转换,**强烈**推荐使用[严格的等于操作符](#types.equality)
22+
虽然这可以避免大部分的问题,但 JavaScript 的弱类型系统仍然会导致一些其它问题。
2723

28-
### Constructors of built-in types
2924

30-
The constructors of the built in types like `Number` and `String` behave
31-
differently when being used with the `new` keyword and without it.
25+
### 内置类型的构造函数(Constructors of built-in types)
3226

33-
new Number(10) === 10; // False, Object and Number
34-
Number(10) === 10; // True, Number and Number
35-
new Number(10) + 0 === 10; // True, due to implicit conversion
27+
内置类型(比如 `Number``String`)的构造函数在被调用时,使用或者不使用 `new` 的结果完全不同。
3628

37-
Using a built-in type like `Number` as a constructor will create a new `Number`
38-
object, but leaving out the `new` keyword will make the `Number` function behave
39-
like a converter.
29+
new Number(10) === 10; // False, 对象与数字的比较
30+
Number(10) === 10; // True, 数字与数字的比较
31+
new Number(10) + 0 === 10; // True, 由于隐式的类型转换
4032

41-
In addition, having literals or non-object values in there will result in even
42-
more type coercion.
33+
使用内置类型 `Number` 作为构造函数将会创建一个新的 `Number` 对象,
34+
而在不使用 `new` 关键字的 `Number` 函数更像是一个数字转换器。
4335

44-
The best option is to cast to one of the three possible types **explicitly**.
36+
另外,在比较中引入对象的字面值将会导致更加复杂的强制类型转换。
4537

46-
### Casting to a string
38+
最好的选择是把要比较的值**显式**的转换为三种可能的类型之一。
39+
40+
41+
### 转换为字符串(Casting to a string)
4742

4843
'' + 10 === '10'; // true
4944

50-
By prepending a empty string a value can easily be casted to a string.
45+
将一个值加上空字符串可以轻松转换为字符串类型。
46+
5147

52-
### Casting to a number
48+
### 转换为数字(Casting to a number
5349

5450
+'10' === 10; // true
5551

56-
Using the **unary** plus operator it is possible to cast to a number.
52+
使用**一元**的加号操作符,可以把字符串转换为数字。
53+
54+
[译者注][30]:字符串转换为数字的常用方法:
55+
56+
+'010' === 10
57+
Number('010') === 10
58+
parseInt('010', 10) === 10 // 用来转换为整数
59+
60+
+'010.2' === 10.2
61+
Number('010.2') === 10.2
62+
parseInt('010.2', 10) === 10
5763

58-
### Casting to a boolean
64+
65+
### 转换为布尔型(Casting to a boolean)
5966

60-
By using the **not** operator twice, a value can be converted a boolean.
67+
通过使用 **** 操作符两次,可以把一个值转换为布尔型。
6168

6269
!!'foo'; // true
6370
!!''; // false

doc/zh/types/instanceof.md

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
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` 操作符用来比较两个操作数的构造函数。只有在比较自定义的对象时才有意义。
4+
如果用来比较内置类型,将会和 [typeof 操作符](#types.typeof) 一样用处不大。
65

7-
### Comparing custom objects
6+
### 比较自定义对象(Comparing custom objects
87

98
function Foo() {}
109
function Bar() {}
@@ -13,28 +12,26 @@ nearly as useless as the [typeof operator](#types.typeof).
1312
new Bar() instanceof Bar; // true
1413
new Bar() instanceof Foo; // true
1514

16-
// This just sets Bar.prototype to the function object Foo
17-
// But not to an actual instance of Foo
15+
// 如果仅仅设置 Bar.prototype 为函数 Foo 本省,而不是 Foo 构造函数的一个实例
1816
Bar.prototype = Foo;
1917
new Bar() instanceof Foo; // false
2018

21-
### Using `instanceof` with native types
19+
### `instanceof` 比较内置类型(Using `instanceof` with native types
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 origin 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` 用来比较属于不同 JavaScript 上下文的对象(比如,浏览器中不同的文档结构)时将会出错,
28+
因为它们的构造函数不会是同一个对象。
3229

33-
### In conclusion
3430

35-
The `instanceof` operator should **only** be used when dealing with custom made
36-
objects that origin from the same JavaScript context. Just like the
37-
[`typeof`](#types.typeof) operator, every other use of it should be **avoided**.
31+
### 结论(In conclusion)
32+
33+
`instanceof` 操作符应该**仅仅**用来比较来自同一个 JavaScript 上下文的自定义对象。
34+
正如 [`typeof`](#types.typeof) 操作符一样,任何其它的用法都应该是避免的。
3835

3936

4037
[30]: http://cnblogs.com/sanshi/

0 commit comments

Comments
 (0)