this深度面试题2

var name = "windows"
var object = {
  name:"object",
  show:function(){
  return function(){
  return this.name  
}  
}        
}

object.show()()   //windows

从此题发现this只有一级往上的作用域(除非特别指定)

window.val = "1"
var obj = {
    val:"2",    
    show: function(){
    this.val *= 2
    val *= 2
    console.log(this.val)
    console.log(val)
}}

obj.show()
var fun = obj.show
fun()

4
2
8
8

 箭头函数的this定义:箭头函数的this是在定义函数时绑定的,不是在执行过程中绑定的。简单的说,函数在定义时,this就继承了定义函数的对象。

猜你喜欢

转载自www.cnblogs.com/-constructor/p/11634797.html