Javascript分配箭头功能说明

时间:2020-02-27 19:11:12

标签: javascript function lambda javascript-objects arrow-functions

Hello Stack Overflow社区,谁能解释我为什么第一种方法可行,为什么第二种返回未定义?

const o = {
  name: "Bob",
  age: 37,
};


/* First Method */

function info() {
   return "Info: " + this.name + " -> " + this.age;
}

o.info = info;

console.log(o.info()); //Info: Bob -> 37

/* Second Method */

o.info = () => {
   return "Info: " + this.name + " -> " + this.age;
}

console.log(o.info); //Undefined

0 个答案:

没有答案