深入理解的一些知识点
读书笔记和心得记录在了我的博客里(还在不断完善):
《你不知道的JavaScript》笔记&心得
【关于 this】
airbnb 有一段:https://github.com/airbnb/javascript
<图片1>
所以,尽早摈弃 self = this; 这种写法,拥抱箭头函数吧!词法作用域比 this 的类似动态作用域特性清晰多了。
另:说说书中几处程序段的不是很明显的错误
【第一处】
见 https://book.douban.com/annotation/37398585/
【第二处】
84页,对严格模式下 this 无法默认绑定为全局对象,作者给了个例子,原文是这样的:
function foo() {
'use strict';
console.log(this.a);
}
var a = 2;
foo(); // TypeError: this is undefined
的确会报错,错误类型的确是 TypeError。但是,报错的内容不是这个样子的。而是:
Uncaught TypeError: Cannot read property 'a' of undefined。也许是作者的运行环境跟我的不一样导致的。
另外,错误内容是 *** is not defined 的话,是RHS过程报错。例如,这样的代码:
function foo() {
'use strict';
console.log(b);
}
foo();
会提示:Uncaught ReferenceError: b is not defined
*** is not defined 和 *** is undefined 的确是有些区别的。。。
《你不知道的JavaScript》笔记&心得
【关于 this】
airbnb 有一段:https://github.com/airbnb/javascript
<图片1>
所以,尽早摈弃 self = this; 这种写法,拥抱箭头函数吧!词法作用域比 this 的类似动态作用域特性清晰多了。
另:说说书中几处程序段的不是很明显的错误
【第一处】
见 https://book.douban.com/annotation/37398585/
【第二处】
84页,对严格模式下 this 无法默认绑定为全局对象,作者给了个例子,原文是这样的:
function foo() {
'use strict';
console.log(this.a);
}
var a = 2;
foo(); // TypeError: this is undefined
的确会报错,错误类型的确是 TypeError。但是,报错的内容不是这个样子的。而是:
Uncaught TypeError: Cannot read property 'a' of undefined。也许是作者的运行环境跟我的不一样导致的。
另外,错误内容是 *** is not defined 的话,是RHS过程报错。例如,这样的代码:
function foo() {
'use strict';
console.log(b);
}
foo();
会提示:Uncaught ReferenceError: b is not defined
*** is not defined 和 *** is undefined 的确是有些区别的。。。
© 本文版权归作者 小王 所有,任何形式转载请联系作者。
有关键情节透露