diff --git a/README.md b/README.md index eb6a241..dcf55d7 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,13 @@ **只是看看的话,对你的帮助没有多大,你需要的是一条一条的实践;理论上,实践过下面的68个项目之后,你的JS能力应该有一个质的飞跃** +**:grin:如果你觉得下面有些章节的代码示例不够好,或者有问题;欢迎发`pull request`或者提`issues`,我会把它添加进来,让更多的人看到。 + 第七章节有几章节没有代码示例,也欢迎大家补充。:grin:** + + [x] Chapter 1 **Accustoming Yourself to JavaScript** - [x] [Item 1: **知道你正在使用的JavaScript模式** (Know Which JavaScript You Are Using)](chapter-1/know-which-javascript-you-are-using.md) - [x] [Item 2: **注意JavaScript的浮点数** (Understand JavaScript’s Floating-Point Numbers)](chapter-1/understand-javascript’s-floating-point-numbers.md) - - [x] [Item 3: **当心隐形的强制转换** (Beware of Implicit Coercions)](chapter-1/beware-of-implicit-coercions.md) + - [x] [Item 3: **当心隐式的强制转换** (Beware of Implicit Coercions)](chapter-1/beware-of-implicit-coercions.md) - [x] [Item 4: **使用原始类型替代对象包裹** (Prefer Primitives to Object Wrappers)](chapter-1/prefer-primitives-to-object-wrappers.md) - [x] [Item 5: **混合类型避免使用`==`比较是否相等** (Avoid using == with Mixed Types)](chapter-1/avoid-using-not-strict-equality-with-mixed-types.md) - [x] [Item 6: **学习分号的插入机制** (Learn the Limits of Semicolon Insertion)](chapter-1/learn-the-limits-of-semicolon-insertion.md) @@ -97,4 +100,4 @@ - \ No newline at end of file + diff --git a/chapter-1/beware-of-implicit-coercions.md b/chapter-1/beware-of-implicit-coercions.md index 81231c6..ffb9d66 100644 --- a/chapter-1/beware-of-implicit-coercions.md +++ b/chapter-1/beware-of-implicit-coercions.md @@ -1,4 +1,4 @@ -### 当心隐形的强制转换 +### 当心隐式的强制转换 ```javascript console.log(3 + true); // 4 @@ -91,4 +91,4 @@ console.log(point()); // { x: 1, y: 1 } + `+`号运算符会根据它的参数类型来决定是做加法还是字符串的拼接。 + `Object`通过它的`toString`方法被强制转换为字符串,通过它的`valueOf`方法被强制转换为数字。 + 带有`valueOf`方法的`Object`应该实现一个`toString`方法,这个`toString`方法返回的字符串就是那个`valueOf`返回的数字的字符串表示形式。 -+ 判断一个值是否是未定义的应该使用`typeof`或者比较的方法,而不是根据这个值表现是`true`或者`false`来判断。 \ No newline at end of file ++ 判断一个值是否是未定义的应该使用`typeof`或者比较的方法,而不是根据这个值表现是`true`或者`false`来判断。 diff --git a/chapter-1/item4/demo.js b/chapter-1/item4/demo.js index baa06b1..798eb5f 100644 --- a/chapter-1/item4/demo.js +++ b/chapter-1/item4/demo.js @@ -1,3 +1,5 @@ +//'use strict'; + var s = new String('hello'); console.log(s); // String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", length: 5, [[PrimitiveValue]]: "hello"} @@ -16,5 +18,6 @@ console.log(s1 == s2); // false console.log(str.toUpperCase()); // HELLO WORLD -str.someProperty = 'some'; +// 下面的语句在严格模式下,会报错: Uncaught TypeError: Cannot create property 'someProperty' on string 'hello world' +str.someProperty = 'some'; // 当然我们也不建议这样做,这里只是用做例子来说明 console.log(str.someProperty); // undefined \ No newline at end of file diff --git a/chapter-1/prefer-primitives-to-object-wrappers.md b/chapter-1/prefer-primitives-to-object-wrappers.md index c7a1537..39aa2a9 100644 --- a/chapter-1/prefer-primitives-to-object-wrappers.md +++ b/chapter-1/prefer-primitives-to-object-wrappers.md @@ -1,6 +1,8 @@ ### 使用原始类型替代对象包裹 ```javascript +//'use strict'; + var s = new String('hello'); console.log(s); // String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", length: 5, [[PrimitiveValue]]: "hello"} @@ -19,7 +21,8 @@ console.log(s1 == s2); // false console.log(str.toUpperCase()); // HELLO WORLD -str.someProperty = 'some'; +// 下面的语句在严格模式下,会报错: Uncaught TypeError: Cannot create property 'someProperty' on string 'hello world' +str.someProperty = 'some'; // 当然我们也不建议这样做,这里只是用做例子来说明 console.log(str.someProperty); // undefined ``` [源码](item4/demo.js) @@ -29,3 +32,7 @@ console.log(str.someProperty); // undefined + 对象包装的原始类型和原始类型在比较相等的时候,它们具有不同的表现行为。 + 在原始类型上获取或者设置属性相当于隐形的创建了一个对象包裹。 + +------ + +感谢[tangxiaolang101](https://github.com/tangxiaolang101)提出的一个[问题](https://github.com/dreamapplehappy/effective-javascript/issues/4) diff --git a/chapter-2/item13/demo.js b/chapter-2/item13/demo.js index 1564566..e8c5f47 100644 --- a/chapter-2/item13/demo.js +++ b/chapter-2/item13/demo.js @@ -46,15 +46,17 @@ function generateFunc2(arr) { var n = arr.length; for(var i = 0; i < n; i++) { (function() { + var j = i; // 注意这里 result[i] = function() { - return arr[i]; + return arr[j]; } })() } return result; } // @3 产生新的函数 -var g3 = generateFunc1(testArr); +//var g3 = generateFunc1(testArr); +var g3 = generateFunc2(testArr); console.log(g3[0]()); // 1 console.log(g3[1]()); // 2 console.log(g3[2]()); // 3 \ No newline at end of file diff --git a/chapter-2/use-immediately-invoked-function-expressions-to-create-local-scopes.md b/chapter-2/use-immediately-invoked-function-expressions-to-create-local-scopes.md index 2da4ee0..d58ddd2 100644 --- a/chapter-2/use-immediately-invoked-function-expressions-to-create-local-scopes.md +++ b/chapter-2/use-immediately-invoked-function-expressions-to-create-local-scopes.md @@ -49,15 +49,17 @@ function generateFunc2(arr) { var n = arr.length; for(var i = 0; i < n; i++) { (function() { + var j = i; // 注意这里 result[i] = function() { - return arr[i]; + return arr[j]; } })() } return result; } // @3 产生新的函数 -var g3 = generateFunc1(testArr); +//var g3 = generateFunc1(testArr); +var g3 = generateFunc2(testArr); console.log(g3[0]()); // 1 console.log(g3[1]()); // 2 console.log(g3[2]()); // 3