1.3 this深度面试题

var big = "1"
var obj = {
big: "2",
showBig: function() {
return this.big
},
}
obj.showBig.call(big)
ƒ big() { [native code] }  //最开始我的疑惑结果为什么不是1
  • obj.showBig.call(big)执行这个的时候,showBig方法中的this指向的是全局参数big 
  • 这个this指向可以参考js function 的this问题
  • 然后是 return this.big ,  由于this是“1” , 所以this.big 相当于 “1”.big .
  • call apply bind 都是改变this指向的【箭头函数不能用,因为箭头函数中的this指向的是函数声明位置的this】
  • class也不能用箭头函数 ,原因待查明
var big = "1"
var obj = {
big: "2",
showBig: function() {
return this.big
},
}
obj.showBig.call(window)   //如果指向window。call()提供新的 this 值给当前调用的函数/方法。  this的值
"1"

 

posted @ 2019-04-10 16:05  容忍君  阅读(566)  评论(0)    收藏  举报