Skip to content

Instantly share code, notes, and snippets.

@guxuerui
Created July 9, 2020 07:52
Show Gist options
  • Save guxuerui/b940c759e29ec342ecb4f9f13571814c to your computer and use it in GitHub Desktop.
Save guxuerui/b940c759e29ec342ecb4f9f13571814c to your computer and use it in GitHub Desktop.
笔试题--JS中的this
var name = 0;
var a = {
name: 1,
f1: function(){
this.name = 2;
},
f2: function(){
this.name = 3;
return null
},
f3: function(){
this.name = 4;
return {}
},
f4: function(){
var name = 5;
console.log(this.name);
},
f5: () => {
var name = 6;
console.log(this.name);
}
}
var o1 = new a.f1;
console.log(o1.name); // 2
var o2 = new a.f2();
console.log(o2.name); // 3
var o3 = new a.f3;
console.log(o3) // 因为f3中return了{},此时会正常返回{},所以o3.name就是undefined
console.log(o3.name); // undefined
a.f4(); // 1
f4 = a.f4;
f4(); // 0
f4.call(a); // 1
console.log(a.f5) // 因为a.f5会返回这个箭头函数() => {var name = 6;console.log(this.name);},而此时箭头函数中的this指向了window,所以a.f5()会返回0
a.f5(); // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment