Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 箭头函数是否不在ES6类内绑定'this'?_Node.js_Ecmascript 6 - Fatal编程技术网

Node.js 箭头函数是否不在ES6类内绑定'this'?

Node.js 箭头函数是否不在ES6类内绑定'this'?,node.js,ecmascript-6,Node.js,Ecmascript 6,我很惊讶这不起作用。(我正在运行带有--harmony\u arrow\u函数标志的iojs2.3.0。) 我希望箭头函数为这个选择正确的值。我遗漏了什么吗?我不知道问题出在哪里,但我的版本对我来说很好: class Foo { constructor() { this.foo = "foo"; } sayHi() { return (() => console.log(this.foo))(); } } const f

我很惊讶这不起作用。(我正在运行带有
--harmony\u arrow\u函数
标志的
iojs
2.3.0。)


我希望箭头函数为
这个
选择正确的值。我遗漏了什么吗?

我不知道问题出在哪里,但我的版本对我来说很好:

class Foo {
    constructor() {
        this.foo = "foo";
    }

    sayHi() {
        return (() => console.log(this.foo))();
    }
}

const f = new Foo();
f.sayHi();

顺便说一句:我正在使用巴别塔,你的生活正在创造一个新的范围
this
指的是IIFE的范围,其中
this.foo
未定义

你解决这个问题的方法就是约束你的生活

class Foo {
    constructor() {
        this.foo = 'foo';
    }
    sayHi() {
        return (() => {
            return this.foo;
        }.bind(this))();
    }
}

let f = new Foo();
console.log(f.sayHi()); //foo

很酷,所以我猜这一定是一个iojs/V8错误。很高兴我发现了这个问题,因为我在几周前遇到了同样的问题。我花了一段时间才发现这是一个V8问题。因为,事实上,巴别塔是有效的。现在我又可以安睡了。但这是我所期望的行为吗?我的理解是箭头函数应该从周围的词法上下文继承它们的
this
值,显然不是。我们在中进行了讨论,显然V8中的箭头函数被破坏了。我在OP的代码中没有看到IIFE(如果你的意思是箭头函数立即执行,那就无关紧要了)@FelixKling你知道IIFE代表什么吗?@FlorianMargaine:是的。
class Foo {
    constructor() {
        this.foo = 'foo';
    }
    sayHi() {
        return (() => {
            return this.foo;
        }.bind(this))();
    }
}

let f = new Foo();
console.log(f.sayHi()); //foo