程序设计之道无远弗届,御晨风而返。———— 杰佛瑞 · 詹姆士
let 、const 和 var 不同。
- 他们具有识别块级作用域的能力,var 函数作用域和全局作用。
- let const 存在一个暂时性死区 (TDZ)[ ] ,无法提升,只能先声明再使用。
- let const 定义的变量无法赋值到 window 。
- let const 不能重复声明。
箭头函数
- 箭头函数式没有 this 没有 arguments surper(class)。
- 箭头函数不可以new 没有构造器 不能当构造函数 prototype。
- 代表数据,数学公式,流向 ==》 , 方向 JavaScript 的引擎的优化。
- 箭头函数具有绑定 this 的天生能力。
this 绑定需要4种办法
- 默认绑定(空函数执行 函数单纯执行,this指向的是 window )。
- 隐式的绑定 (谁调用 this 指向谁) 第三权重
- 显示的绑定 (call apply , bind ) 改变 this 的指向 第二权重。
- new 绑定 this 权重最高。
这里圈重点 bind只能改变一次 this 指向。
let newPrint = obj.print.bind( window );
let newnewPrint = newPrint.bind(obj) ; // bind只能绑定一次
附赠检测数据类型代码 :
//es5
Object.prototype.toString.call([]);
function isType(type){
return function(obj){
return Object.prototype.toString.call(obj) === "[object" + type + "]";
}
}
//es6
//普通版
let isType = type => obj => Object.prototype.toString.call(obj) === "[object" + type + "]";
//加强版
let isType = type => obj => Object.prototype.toString.call(obj) === `[object ${type} ]`;
//终极版
let isType = type => obj => Object.is(Object.prototype.toString.call(obj) === `[object ${type} ]`);