ES6专题 class中函数的指向问题的两种解决方案

class Person{
        constructor(){
            this.eat = this.eat.bind(this)//第二种方式
        }
        eat(){
            // 调用get
            this.get()
        }
       //第一种方式
        // eat = ()=>{
        //     this.get()
        // }
        get(){
            console.log('我被调用了');
        }
            
    }
    let person1 = new Person()
    // person1.eat()

    let {eat} = person1
    // let eat = person1.eat
    eat() // 不会输出主要是this指向问题
原创文章 207 获赞 173 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41181778/article/details/104807760