简短的答案:
this指向最接近的边界
this-所提供的代码
this位于封闭范围内。
更长的答案:箭头功能 没有this
创建它们时将其绑定
this,
arguments或结合其它特殊名字都正在创建对象时的名称
this是在封闭的范围内,没有找到
person对象。您可以通过移动声明来更清楚地看到这一点:
var person = { name: "Jason"};person.shout = () => console.log("Hi, my name is", this);并且在翻译成ES5中箭头语法的模糊近似时更加清晰:
var person = { name: "Jason"};var shout = function() { console.log("Hi, my name is", this.name);}.bind(this);person.shout = shout;在这两种情况下,
this(对于shout函数)都指向与其中
person定义的作用域相同的函数,而不是将函数添加到
person对象时附加的新作用域。
您 不能
使箭头函数那样工作,但是,正如@kamituel在他的答案中指出的那样,您可以利用ES6中较短的方法声明模式来节省相似的空间:
var person = { name: "Jason", // ES6 "method" declaration - leave off the ":" and the "function" shout() { console.log("Hi, my name is", this.name); }};


