From 2bbd242488b4bd67804ad178f6b073511fa1352c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E5=87=AF?= Date: Tue, 15 Nov 2016 11:37:11 +0800 Subject: [PATCH 1/7] fix typo --- chapter-1/beware-of-implicit-coercions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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`来判断。 From 64783ae2f96dbda2a07c8a334eb49fcac9681285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E5=87=AF?= Date: Tue, 15 Nov 2016 11:37:40 +0800 Subject: [PATCH 2/7] fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eb6a241..4db14b8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ + [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 +97,4 @@ - \ No newline at end of file + From 4d8f1b9e04c782e6b2105c2daefe97389327a326 Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Tue, 15 Nov 2016 21:28:23 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9item13=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chapter-2/item13/demo.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/chapter-2/item13/demo.js b/chapter-2/item13/demo.js index 1564566..b743e35 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 From e69b532acbdb1a815f633ef9aa677520a94814be Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Tue, 15 Nov 2016 21:31:21 +0800 Subject: [PATCH 4/7] fix item13 fix item13 --- chapter-2/item13/demo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter-2/item13/demo.js b/chapter-2/item13/demo.js index b743e35..e8c5f47 100644 --- a/chapter-2/item13/demo.js +++ b/chapter-2/item13/demo.js @@ -46,7 +46,7 @@ function generateFunc2(arr) { var n = arr.length; for(var i = 0; i < n; i++) { (function() { - var j = i; + var j = i; // 注意这里 result[i] = function() { return arr[j]; } From 420835c3172d5ee03adbaae77fdeb1405ed7f95d Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Tue, 15 Nov 2016 21:33:43 +0800 Subject: [PATCH 5/7] fix item13 --- ...y-invoked-function-expressions-to-create-local-scopes.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 From 3bde49a31c67ee17d60513587671ff107fa18ef4 Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Thu, 17 Nov 2016 09:34:54 +0800 Subject: [PATCH 6/7] add some explanation 0 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 4db14b8..dcf55d7 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ **只是看看的话,对你的帮助没有多大,你需要的是一条一条的实践;理论上,实践过下面的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) From a6b0ee558504d1d7bea0556ed35d747da3b5f9cf Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Wed, 23 Nov 2016 09:22:44 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20chapter1=20item4?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=B8=AA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chapter-1/item4/demo.js | 5 ++++- chapter-1/prefer-primitives-to-object-wrappers.md | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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)