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. js用canvans 实现简单的粒子运动

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. 自己实现一个nullptr

    一 具体实现 代码(c++) const class nullptr_t { public: template<class T> inline operator T*() const { ...

  3. WPF中查看PDF文件之MoonPdfLib类库

    最近研究了两种PDF文件查看器,MoonPdfLib或者AdobeReader. 今天先说第一种,在网上扒到的很好的WPF中用MoonPdf类库来展示PDF文件. 在Sourceforge上下载到Mo ...

  4. H5播放器内置播放视频(兼容绝大多数安卓和ios)

    关于H5播放器内置播放视频,这个问题一直困扰我很长一段时间,qq以前提供白名单已经关闭,后来提供了同层属性的控制,或多或少也有点差强人意. 后来一次偶然发现一个非常简单的方法可以实现. 只需要给vid ...

  5. Linux:Day18(下) Bind9

    子域授权:每个域的名称服务器,都是通过其上级名称服务器在解析库中进行授权. 类似根域授权tld: .com IN NS ns1.com. .com IN NS ns1.com. ns1.com IN ...

  6. php将字符串转为二进制数据串

    /** * 将字符串转换成二进制 * @param type $str * @return type */ function StrToBin($str){ //1.列出每个字符 $arr = pre ...

  7. Module build failed: Error: Cannot find module 'babel-runtime/core-js/get-it

    npm i babel-loader@7.1.5 -D

  8. Python3.7源码在windows(VS2015)下的编译和安装

    Python3.7源码在windows(VS2015)下的编译和安装 下载官方源码,使用vs2015(WIN10SDK),最python3.7.0的源码进行编译,编译出不同的版本(release,de ...

  9. Appium环境搭建-精简版

    Appium自动化环境准备 安装配置JDK 下载Android SDK并配置环境变量 安装模拟器或连接真机 安装appium desktop 安装python和pycharm (开发语言和开发工具) ...

  10. tornado之用户验证装饰器

    authenticated装饰器 为了使用Tornado的认证功能,我们需要对登录用户标记具体的处理函数.我们可以使用@tornado.web.authenticated装饰器完成它.当我们使用这个装 ...