闭包、this
闭包特征:
a.函数作为参数传递
b.函数作为返回值
闭包的自由变量查找是在函数定义的地方向上级作用域查找,而不是执行的地方
this是在函数执行的地方查找,而不是定义的地方,他俩正好相反
this五种情况:普通函数;call,apply,bind;对象方法;类;箭头函数
箭头函数this的取值是取其上级作用域(即定义的位置)里this的值
function a () { alert (this); } a.call (null); a.call(undefined); a.call();
1、在ES5的严格模式中,无论传入的是null还是undefined,严格按照定义来,即“第一个参数是调用函数的母对象”
a.call (null);//null a.call(undefined); //undefined a.call();//undefined
2、在非严格模式即ES3中,传入的是null、undefined或不传入参数时,都会用全局对象来代替。
a.call (null);//[Object window] a.call(undefined); //[Object window] a.call();//[Object window]
apply同上。