JavaScript 双冒号(绑定(bind)运算符)

标签 javascript ecmascript-harmony ecmascript-next

如您所知,有一个关于.bind() 函数的快捷方式的提议,因此您可以这样写:

::this.handleStuff

它会像在 es5 中那样工作:

this.handleStuff.bind(this)

我的问题是:是否可以通过这种方式传递参数?

我的意思是用前面提到的快捷方式写这个:

this.handleStuff.bind(this, 'stuff')

这是 React 中非常常见的模式,因此将其缩短一点会很好。

最佳答案

没有。 bind operator ( spec proposal ) 有两种形式:

  • 方法提取

    ::obj.method     ≡ obj.method.bind(obj)
    
  • “虚方法”调用

    obj::function    ≡ function.bind(obj)
    obj::function(…) ≡ function.call(obj, …)
    

它们都没有 partial application .对于你想要的,你应该使用箭头函数:

(...args) => this.handleStuff('stuff', ...args) ≡ this.handleStuff.bind(this, 'stuff')

关于JavaScript 双冒号(绑定(bind)运算符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220078/

相关文章:

javascript - ES6导出默认的AssignmentExpression

javascript - ES6 : Accessing static member variables in classes created from Mixin

javascript - ESnext 中的属性初始化器语法

javascript - Angular:将动态元素滚动到只知道其 ID 的 View 中

javascript - 有没有办法以我的示例通过 React Native Expo 来执行后台任务?

javascript - 非法调用 document.querySelector

javascript - 从任意 HTML 表创建工具提示输出

javascript - 浏览器如何区分ES5和ES6脚本?

javascript - ES7 装饰器规范是否要求描述符具有 `initializer` 方法

reactjs - 使对象传播以使用 webpack.config 和 babelrc 文件