此上下文在javascript对象中包含范围和上下文

此上下文在javascript对象中包含范围和上下文,javascript,Javascript,**据我所知,这是指在**中创建它们的上下文 2这就是为什么在我的匿名函数中,this.name工作正常 const user = { name:'mdv', places : ['rio', 'new', 'aust'], printPlacesLived: function () { return this.places.map((place)=>this.name +'has lived in '+place) } } 同样的方法,我

**据我所知,这是指在**中创建它们的上下文

2这就是为什么在我的匿名函数中,this.name工作正常

const user = {
    name:'mdv',
    places : ['rio', 'new', 'aust'],
    printPlacesLived: function () {
        return this.places.map((place)=>this.name +'has lived in '+place)
    }
}
同样的方法,我已经将PrintPlaces剪切为箭头函数,因此根据定义

这是指在其中创建它们的上下文

1现在我得到了未定义的位置如果定义正确,printPlacesLived是在我的用户对象中创建的,所以引用必须是位置,但没有发生为什么

2我很困惑,我研究了箭头函数的一些其他定义,这一点有人提到,它保留了封闭词汇上下文的值

现在,对于printPlacesLived,词汇上下文是名称和位置,请帮助我,我哪里出错了

您的1代码运行良好,需要一些更正 在arrow函数中,这是为了升级
与常规函数不同,箭头函数没有自己的函数。箭头函数中的此值在函数的整个生命周期中保持不变,并且始终绑定到最近的非箭头父函数中的此值

const user = {
    name:'mdv',
    places : ['rio', 'new', 'aust'],
    printPlacesLived:  () =>{
        return this.places.map((place)=>this.name +'has lived in '+place)
    }
}

我已经改变了问题,请通读一遍,不要在别人回答了你的问题之后再完全改变你的问题。我宁愿问一个新问题。对不起,但现在它更有意义了,因为我已经试着用小代码来表达我的怀疑,就像我说的,请问一个新问题。人们已经把他们的精力放在了原来的问题上,这个编辑完全改变了问题,使现有的答案无效。好的,但我不知道如何回退我可以帮你做。您可以通过单击问题下方已编辑的链接来查找版本。
let me = { 
 name: "Ashutosh Verma", 
 thisInArrow:() => { 
 console.log("My name is " + this.name); // no 'this' binding here 
 }, 
 thisInRegular(){ 
 console.log("My name is " + this.name); // 'this' binding works here 
 } 
};
me.thisInArrow(); 
me.thisInRegular();