箭头函数中的this没有作用域 只能向外找 就是window
想要this指向vue let self = this
this
在方法中 this 表示该方法所属的对象
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
person.fullName() //'John Doe' 此时 this 指向的是 person
如果单独使用,this表示全局对象
let x = this
x// Window
在函数中,this表示全局对象
function myFunction() {
// "use strict" 严格模式下 this 指向 undifined
return this;
}
myFunction() // Window
事件中,this表示接受事件的元素
<button onclick="this.style.display='none'"> //this 指向这个button
点我后我就消失了
</button>
类似 call() 和 apply() 方法可以将this引用到任何对象
var person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
var person2 = {
firstName:"John",
lastName: "Doe",
}
person1.fullName.call(person2); // 返回 "John Doe"