arrow function and bind
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.
bindstill 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的更多相关文章
- vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug
vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...
- bind & this & new & arrow function
bind & this & new & arrow function this bind call apply new arrow function arrow functio ...
- JavaScript学习笔记(十二)——箭头函数(Arrow Function)
在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
- 廖雪峰js教程笔记5 Arrow Function(箭头函数)
为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数 阅读: ...
- 【转载】C++ function、bind和lambda表达式
本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制 ...
- 一起Polyfill系列:Function.prototype.bind的四个阶段
昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...
- 聊聊Function的bind()
bind顾名思义,绑定. bind()方法会创建一个新函数,当这个新函数被调用时,它的this值是传递给bind()的第一个参数,它的参数是bind()的其他参数和其原本的参数. 上面这个定义最后一句 ...
- Function.prototype.bind接口浅析
本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...
随机推荐
- [20190423]简单测试latch nowilling等待模式.txt
[20190423]简单测试latch nowilling等待模式.txt --//我对这个问题的理解就是如果参数willing=0,表示无法获取该latch,直接退出,再寻找类似的latch.--/ ...
- 国产多维数据库 NeuralCube!中国人自己的大数据底层核心技术!
商业转载请联系作者获得授权,非商业转载请注明出处. 提到‘数据库’,首先被想到的肯定是Oracle.DB2.SQL Server.MySql这些传统的关系型数据库.数据库的概念是非常宽泛的,除了上述的 ...
- C# Base64方式的编码与解码
编码与解码方法: ///编码 public static string EncodeBase64(string code_type, string code) { string encode = &q ...
- svn + nginx unit + python3自动化发布web服务方法
本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...
- Map根据value值进行倒序排列
public List<Map.Entry<Integer,BigDecimal>> sortByMapValue(HashMap<Integer, BigDecimal ...
- [POI2015]PUS
嘟嘟嘟 这题只要往正确的方面想,就很简单. 首先,这是一道图论题! 想到这,这题就简单了.对于两个数\(i\)和\(j\),如果\(i\)比\(j\)大,就从\(i\)向\(j\)连边.然后如果图中存 ...
- php解决微信文章图片防盗链
解决微信文章图片防盗链 function actionWechatImg() { header('Content-type: image/jpg'); $url = $_GET['url']; $re ...
- Java中数组和集合的foreach操作编译后究竟是啥
今天和同事在关于foreach编译后是for循环还是迭代器有了不同意见,特做了个Demo,了解一下. 是啥自己来看吧! public class Demo { public static void m ...
- Win 10 Edge不能上网,微软商店打不开的问题
微软商店(Microsoft Store)的 Code: 0x80072EE7 Check Your Connection问题,和Edge的Can't Reach this page的问题.网上找了很 ...
- 简单了解python使用正则表达式
正则[Regular Expression]:正则表达式通常被用来检索.替换那些符合某个模式(规则)的文本. 正则是用来干啥的:正则就是用来匹配字符串的. Python中string的几个方法: fi ...