箭头函数返回未定义

时间:2020-04-11 22:04:12

标签: javascript node.js reactjs ecmascript-6 v8

我想了解为什么这段代码返回一个值,而第二个代码返回undefined;

function personName(){
  console.log(this.name);
}

const obj = 
{
    name: "David",
    greetings: function() {
      return `Hello ${this.name}`
    }
}

console.log( obj.greetings() );

function personName(){
  console.log(this.name);
}

const obj = {
  name: "David",
  greetings: () => `Hello ${this.name}`
}

console.log( obj.greetings() );

0 个答案:

没有答案