小编Fac*_*ion的帖子

ES6中的super.method上下文处理

由于Javascript中还没有私有方法支持,我通常只是在类体外声明一个普通函数并提供this参数:

class Foo {
    constructor() {
        _doPrivateStuff(this);
    }
}

function _doPrivateStuff(that) {
    that.bar() // private stuff
}

export default Foo;
Run Code Online (Sandbox Code Playgroud)

但是我怎么能以这种方式接受超级方法呢?

function _doPrivateStuff(that) {
    super.method(); // error, 'super' outside of function or class
}
Run Code Online (Sandbox Code Playgroud)

除此之外,有一个很好的理由,为什么我不应该用这样的"私人"的功能呢?

顺便说一句,我只在Babel上试过这个,而使用_doPrivateStuff.bind(this)()而不是使用that也不起作用

javascript this super ecmascript-6

2
推荐指数
1
解决办法
524
查看次数

标签 统计

ecmascript-6 ×1

javascript ×1

super ×1

this ×1