【发布时间】:2016-10-31 12:44:33
【问题描述】:
我开始学习 ES6 并编写了这段代码:
let dog = {
s:'Woff',
talk:()=>{console.log(this.s)}
};
dog.talk();
let x = dog.talk;
x();
x.bind(dog)();
为什么它总是返回 undefined 而不是 'Woff' ?我认为只有 1 行返回 undefined 但我们有 3 undefined 。
【问题讨论】:
-
console.log 返回未定义。
-
1.你这里没有任何
return语句,2. 你console.log(console.log(...)) -
@JaredSmith 刚刚检查了文档,因为他有
{}(块体),据我所知,他需要一个回报:Arrow functions -
我认为每个人都对这里“返回”这个词的使用读得太多了……stackoverflow.com/q/33723263/476 是一个非常好的复制品。
-
@CodingIntrigue 在 FunctionBody 下:
var func = (x, y) => { return x + y; }; // with block body, explicit "return" needed