Can you bind arrow functions?

https://stackoverflow.com/questions/33308121/can-you-bind-arrow-functions

 

You cannot "rebind" an arrow function. It will always be called with the context in which it was defined. Just use a normal function.

From the ECMAScript 2015 Spec:

Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function.

箭头函数中的this与词法域绑定, 即使使用bind企图改变this,也是徒劳的。

To be complete, you can re-bind arrow functions, you just can't change the meaning of this.

bind still has value for function arguments:

((a, b, c) => {
console.info(a, b, c) // 1, 2, 3
}).bind(undefined, 1, 2, 3)()

Try it here: http://jsbin.com/motihanopi/edit?js,console

箭头函数this确定, 普通函数中的this,由调用场景/对象确定。

如果也想模拟arrow函数效果, 可以使用bind接口,在定义的时候, 就把this绑定到词法中的this:

function (){

}.bind(this)

箭头函数不该使用场景

https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

Defining methods on an object

Callback functions with dynamic context

Invoking constructors

Too short syntax

箭头函数该使用场景

函数短小

函数式计算的入参

需要确定this为词法域的

https://www.imooc.com/article/20607

arrow function and bind的更多相关文章

  1. vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug

    vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...

  2. bind & this & new & arrow function

    bind & this & new & arrow function this bind call apply new arrow function arrow functio ...

  3. JavaScript学习笔记(十二)——箭头函数(Arrow Function)

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...

  4. ES6 new syntax of Arrow Function

    Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...

  5. 廖雪峰js教程笔记5 Arrow Function(箭头函数)

    为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数 阅读: ...

  6. 【转载】C++ function、bind和lambda表达式

    本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制 ...

  7. 一起Polyfill系列:Function.prototype.bind的四个阶段

    昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...

  8. 聊聊Function的bind()

    bind顾名思义,绑定. bind()方法会创建一个新函数,当这个新函数被调用时,它的this值是传递给bind()的第一个参数,它的参数是bind()的其他参数和其原本的参数. 上面这个定义最后一句 ...

  9. Function.prototype.bind接口浅析

    本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...

随机推荐

  1. oracle知识点总结基础篇1

    最近学习了Oracle,对学习内容挑干的进行总结! 1.准备工作:学习Oracle首先就是安装环境.我装的是oracle11g. 2.安装完成之后在dos窗口中,输入  sqlplus 再输入用户名和 ...

  2. WEB 性能测试用例设计以及总结

    WEB 性能测试用例设计以及总结 WEB 性能测试用例设计模型是设计性能测试用例的一个框架,在实际项目中,需要对其进行适当的剪裁,从而确定性能测试用例的范围和类别.剪裁的依据是性能测试策略和测试范围, ...

  3. ubuntu18.04安装spark(伪分布式)

    在安装spark之前,首先需要安装配置Hadoop,这个就不做详细介绍了,可以参看博主的另一篇: https://blog.csdn.net/weixin_42001089/article/detai ...

  4. 什么是 CI/CD?

    什么是 CI/CD? 在软件开发中经常会提到持续集成Continuous Integration(CI)和持续交付Continuous Delivery(CD)这几个术语.但它们真正的意思是什么呢? ...

  5. win 10 精简组件列表

    轻松访问工具 操作中心 应用程序虚拟化(App-V) Telemetry Client (Asimov)(遥测) Assigned Access(按需访问) 自动播放 网络后台传输 备份 生物识别服务 ...

  6. 毕业设计(1)基于MicroPython的大棚监测控制系统的程序设计与模型设计

    智慧农业就是将物联网技术运用到传统农业中去,运用传感器和软件通过移动平台或者电脑平台对农业生产进行控制,使传统农业更具有“智慧”.除了精准感知.控制与决策管理外,从广泛意义上讲,智慧农业还包括农业电子 ...

  7. 随心测试_软测基础_006<测试人职业发展>

    接上篇:熟悉了_测试人员的工作职责范围与具体的工作内容 ,如何规划:测试人员的职业路线呢? 贴心小提示:以下内容,仅供参考,不挖坑 Q1:如何规划测试工程师的职业发展路线? A1:SX的观点:预定目标 ...

  8. VSCode python 遇到的问题:vscode can't open file '<unprintable file name>': [Errno 2] No such file or directory

    代码很简单,就两行: import pandas as pd import netCDF4 as nc dataset = nc.Dataset('20150101.nc') 环境:在VSCode中左 ...

  9. Swoole入门到实战打造高性能赛事直播平台(完整版)

    需要 的联系我,QQ:1844912514

  10. MySQL 5.6 for Windows 解压缩版配置安装 和 MySQL 数据库的安装和密码的设定

    https://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html https://jingyan.baidu.com/article/09 ...