this 指向2 构造函数中的this

构造函数中的 this

构造函数中的 this 指向, 指向这个构造函数的 实例对象(比较单一)
但是可以使用 call apply bind 去改变 this 的指向 如何通过这三个方法改变 this

  1. 不改变 this 指向
function Person() {
    
    
	this.name = '张三'
    this.move = function () {
    
    
        move()
    }
}
function move() {
    
    
    console.log(this)
}
let p1 = new Person()
p1.move()//Window 对象
move()//Window 对象
  1. 改变 this 指向
function Person() {
    
    
	this.name = '张三'
    this.move = function () {
    
    
        move.call(this)
    }
}
function move() {
    
    
    console.log(this)
}
let p1 = new Person()
p1.move()//打印结果 p1 这个对象
move()//Window 对象

猜你喜欢

转载自blog.csdn.net/Chennfengg222/article/details/104692778