From 2bbd242488b4bd67804ad178f6b073511fa1352c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E5=87=AF?= Date: 2016年11月15日 11:37:11 +0800 Subject: [PATCH 01/14] 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: 2016年11月15日 11:37:40 +0800 Subject: [PATCH 02/14] 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: 2016年11月15日 21:28:23 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E4=BF=AE=E6=94=B9item13=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=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: 2016年11月15日 21:31:21 +0800 Subject: [PATCH 04/14] 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: 2016年11月15日 21:33:43 +0800 Subject: [PATCH 05/14] 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: 2016年11月17日 09:34:54 +0800 Subject: [PATCH 06/14] 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: 2016年11月23日 09:22:44 +0800 Subject: [PATCH 07/14] =?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) From a21859c47e6eee03e057f62662c029b1a971cd82 Mon Sep 17 00:00:00 2001 From: sheldon shen Date: 2017年2月19日 09:33:06 +0800 Subject: [PATCH 08/14] fix special variable name 'top' bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uncaught TypeError: top.addChild is not a function,参考链接http://blog.csdn.net/u010689306/article/details/53130367 --- .../store-instance-state-only-on-instance-objects.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chapter-4/store-instance-state-only-on-instance-objects.md b/chapter-4/store-instance-state-only-on-instance-objects.md index a974534..bb01ec0 100644 --- a/chapter-4/store-instance-state-only-on-instance-objects.md +++ b/chapter-4/store-instance-state-only-on-instance-objects.md @@ -19,9 +19,9 @@ console.log(left.children); // [ 2, 3 ] var right = new Tree(4); right.addChild(5); right.addChild(6); -var top = new Tree(7); -top.addChild(left); -top.addChild(right); +var middle = new Tree(7); +middle.addChild(left); +middle.addChild(right); console.log(left.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] console.log(top.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] @@ -54,4 +54,4 @@ console.log(top1.children); // [ { value: 1, children: [ 2, 3 ] },{ value: 4, ch ### 源码 + **共享可变数据可能会出现问题,因为原型是被所有的实例共享的。** -+ **将可变的实例状态存储在实例对象中。** \ No newline at end of file ++ **将可变的实例状态存储在实例对象中。** From 3f312df2f9e4a9a65a83b5fcc2ca415902203f47 Mon Sep 17 00:00:00 2001 From: sheldon shen Date: 2017年2月19日 09:35:56 +0800 Subject: [PATCH 09/14] fix special variable name 'top' bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uncaught TypeError: top.addChild is not a function,参考链接http://blog.csdn.net/u010689306/article/details/53130367 --- chapter-4/item36/demo.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chapter-4/item36/demo.js b/chapter-4/item36/demo.js index a7b43d3..5d8238d 100644 --- a/chapter-4/item36/demo.js +++ b/chapter-4/item36/demo.js @@ -16,9 +16,9 @@ console.log(left.children); // [ 2, 3 ] var right = new Tree(4); right.addChild(5); right.addChild(6); -var top = new Tree(7); -top.addChild(left); -top.addChild(right); +var middle = new Tree(7); +middle.addChild(left); +middle.addChild(right); console.log(left.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] console.log(top.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] From 8e7f2f841f4c826f5b56f23f8615416d25099314 Mon Sep 17 00:00:00 2001 From: Yao Date: 2018年1月24日 22:10:57 +0800 Subject: [PATCH 10/14] update top things MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit top 对象是没有 children 属性的 应该是 left right middle 之一 --- chapter-4/store-instance-state-only-on-instance-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter-4/store-instance-state-only-on-instance-objects.md b/chapter-4/store-instance-state-only-on-instance-objects.md index bb01ec0..839b314 100644 --- a/chapter-4/store-instance-state-only-on-instance-objects.md +++ b/chapter-4/store-instance-state-only-on-instance-objects.md @@ -23,7 +23,7 @@ var middle = new Tree(7); middle.addChild(left); middle.addChild(right); console.log(left.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] -console.log(top.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] +console.log(right.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] // @2 正确的实现方式 将状态存储在每个实例中 function Tree1(value) { From e023f3bff1f315dea9b44b43f85b6d11fefab5d5 Mon Sep 17 00:00:00 2001 From: Yao Date: 2018年1月24日 22:13:26 +0800 Subject: [PATCH 11/14] Update demo.js same as previous commit; --- chapter-4/item36/demo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter-4/item36/demo.js b/chapter-4/item36/demo.js index 5d8238d..fb07f77 100644 --- a/chapter-4/item36/demo.js +++ b/chapter-4/item36/demo.js @@ -20,7 +20,7 @@ var middle = new Tree(7); middle.addChild(left); middle.addChild(right); console.log(left.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] -console.log(top.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] +console.log(right.children); // [ 2, 3, 5, 6, { value: 1 }, { value: 4 } ] // @2 正确的实现方式 将状态存储在每个实例中 function Tree1(value) { From 366944f22791fb91289fd5c3e1af26b9ecb0fa29 Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: 2016年11月23日 09:29:59 +0800 Subject: [PATCH 12/14] update --- chapter-1/item4/demo.html | 1 - 1 file changed, 1 deletion(-) diff --git a/chapter-1/item4/demo.html b/chapter-1/item4/demo.html index 8f34e7e..9e59293 100644 --- a/chapter-1/item4/demo.html +++ b/chapter-1/item4/demo.html @@ -6,6 +6,5 @@
-

AltStyle によって変換されたページ (->オリジナル) /

\ No newline at end of file From e5c37358794ca147963f13591ba2635df9dc5ff8 Mon Sep 17 00:00:00 2001 From: dreamapple <2451731631@qq.com> Date: Thu, 1 Feb 2018 10:43:29 +0800 Subject: [PATCH 13/14] fix issue 7 --- chapter-5/item45/demo.js | 2 +- ...use-hasOwnProperty-to-protect-against-prototype-pollution.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter-5/item45/demo.js b/chapter-5/item45/demo.js index 2849ef7..e3ab81d 100644 --- a/chapter-5/item45/demo.js +++ b/chapter-5/item45/demo.js @@ -19,7 +19,7 @@ Dict.prototype.has = function(key) { return Object.hasOwnProperty.call(this.elements, key); }; Dict.prototype.get = function(key) { - return this.elements.has(key) ? this.elements[key] : undefined; + return this.has(key) ? this.elements[key] : undefined; }; Dict.prototype.set = function(key, value) { this.elements[key] = value; diff --git a/chapter-5/use-hasOwnProperty-to-protect-against-prototype-pollution.md b/chapter-5/use-hasOwnProperty-to-protect-against-prototype-pollution.md index cd759ed..50783d1 100644 --- a/chapter-5/use-hasOwnProperty-to-protect-against-prototype-pollution.md +++ b/chapter-5/use-hasOwnProperty-to-protect-against-prototype-pollution.md @@ -22,7 +22,7 @@ Dict.prototype.has = function(key) { return Object.hasOwnProperty.call(this.elements, key); }; Dict.prototype.get = function(key) { - return this.elements.has(key) ? this.elements[key] : undefined; + return this.has(key) ? this.elements[key] : undefined; }; Dict.prototype.set = function(key, value) { this.elements[key] = value; From 129fe247c6e6d84887a1c7e00a21b81da206b3d1 Mon Sep 17 00:00:00 2001 From: xieda Date: 2018年2月10日 10:18:52 +0800 Subject: [PATCH 14/14] fix link of item 68 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dcf55d7..8fa4170 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ - [x] [item 65: **不要在计算时阻塞事件队列** (Don’t Block the Event Queue on Computation)](chapter-7/do-not-block-the-event-queue-on-computation.md) - [x] [item 66: **使用计数器来执行并行操作** (Use a Counter to Perform Concurrent Operations)](chapter-7/use-a-counter-to-perform-concurrent-operations.md) - [x] [item 67: **绝不要同步地调用异步的回调函数** (Never Call Asynchronous Callbacks Synchronously)](chapter-7/never-call-asynchronous-callbacks-synchronously.md) - - [x] [item 68: **(非常值得自己实践)使用`promise`模式清洁异步逻辑** (Use Promises for Cleaner Asynchronous Logic)](chapter-7/) + - [x] [item 68: **(非常值得自己实践)使用`promise`模式清洁异步逻辑** (Use Promises for Cleaner Asynchronous Logic)](chapter-7/use-promises-for-cleaner-asynchronous-logic.md) ------