master-of-javascript-memory icon indicating copy to clipboard operation
master-of-javascript-memory copied to clipboard

第二章章中可能有一点小错误

Open wayne-liberty opened this issue 6 years ago • 1 comments

文章中这句话 ”而这个函数恰好是一个箭头函数,它的this指向正是Vue的实例“, 箭头函数是没有 this 这个属性的, 这个地方可能有点问题. 箭头函数里面的this只是普通的闭包引用. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

wayne-liberty avatar Apr 22 '19 01:04 wayne-liberty

mounted() {
    window.addEventListener('resize', () => {
        this.width = window.innerWidth;
        this.height = window.innerHeight;
    });
}

这段代码中,mounted() 函数内部的this的指向是Vue实例,而在这个函数内部的箭头函数,就恰好指向mounter()函数内的this指向,因此它也就指向Vue实例了。

箭头函数内的this --> mounted()函数内的this --> Vue instance

andycall avatar May 16 '19 19:05 andycall