js this - ythy/blog GitHub Wiki
this 的scope测试:
var b = 10
function t(){
console.log(1, this.b);
var a = {
b: 2,
c: function(){
console.log(2, this.b);
}
}
function d(){
console.log(3, this.b);
}
a.c();
d();
}
t.call({b:5}); //5 2 10
通过call等调用函数 函数内嵌套函数d()的作用域不在call范围!! 而是global
把d() 改为arrow形式, arrow函数内的this和 函数外function this统一:
var b = 10
function t(){
console.log(1, this.b);
var a = {
b: 2,
c: function(){
console.log(2, this.b);
}
}
d = ()=>{
console.log(3, this.b);
}
a.c();
d();
}
t.call({b:5}); //5 2 5