箭头函数与function的区别
来源:互联网 发布:知乎远方光电怎么样 编辑:程序博客网 时间:2024/05/15 14:07
原文地址:http://es6.ruanyifeng.com/#docs/function#箭头函数
箭头函数有几个使用注意点。
(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。
(3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。
(4)不可以使用yield命令,因此箭头函数不能用作 Generator 函数。
上面四点中,第一点尤其值得注意。this对象的指向是可变的,但是在箭头函数中,它是固定的。
function foo() { setTimeout(() => { console.log('id:', this.id); }, 100);}var id = 21;foo.call({ id: 42 });// id: 42
上面代码中,setTimeout
的参数是一个箭头函数,这个箭头函数的定义生效是在foo
函数生成时,而它的真正执行要等到100毫秒后。如果是普通函数,执行时this
应该指向全局对象window
,这时应该输出21
。但是,箭头函数导致this
总是指向函数定义生效时所在的对象(本例是{id: 42}
),所以输出的是42
。
箭头函数可以让setTimeout
里面的this
,绑定定义时所在的作用域,而不是指向运行时所在的作用域。下面是另一个例子。
function Timer() { this.s1 = 0; this.s2 = 0; // 箭头函数 setInterval(() => this.s1++, 1000); // 普通函数 setInterval(function () { this.s2++; }, 1000);}var timer = new Timer();setTimeout(() => console.log('s1: ', timer.s1), 3100);setTimeout(() => console.log('s2: ', timer.s2), 3100);// s1: 3// s2: 0
上面代码中,Timer
函数内部设置了两个定时器,分别使用了箭头函数和普通函数。前者的this
绑定定义时所在的作用域(即Timer
函数),后者的this
指向运行时所在的作用域(即全局对象)。所以,3100毫秒之后,timer.s1
被更新了3次,而timer.s2
一次都没更新。
箭头函数可以让this
指向固定化,这种特性很有利于封装回调函数。下面是一个例子,DOM 事件的回调函数封装在一个对象里面。
var handler = { id: '123456', init: function() { document.addEventListener('click', event => this.doSomething(event.type), false); }, doSomething: function(type) { console.log('Handling ' + type + ' for ' + this.id); }};
上面代码的init
方法中,使用了箭头函数,这导致这个箭头函数里面的this
,总是指向handler
对象。否则,回调函数运行时,this.doSomething
这一行会报错,因为此时this
指向document
对象。
this
指向的固定化,并不是因为箭头函数内部有绑定this
的机制,实际原因是箭头函数根本没有自己的this
,导致内部的this
就是外层代码块的this
。正是因为它没有this
,所以也就不能用作构造函数。
所以,箭头函数转成 ES5 的代码如下。
// ES6function foo() { setTimeout(() => { console.log('id:', this.id); }, 100);}// ES5function foo() { var _this = this; setTimeout(function () { console.log('id:', _this.id); }, 100);}
上面代码中,转换后的ES5版本清楚地说明了,箭头函数里面根本没有自己的this
,而是引用外层的this
。
请问下面的代码之中有几个this
?
function foo() { return () => { return () => { return () => { console.log('id:', this.id); }; }; };}var f = foo.call({id: 1});var t1 = f.call({id: 2})()(); // id: 1var t2 = f().call({id: 3})(); // id: 1var t3 = f()().call({id: 4}); // id: 1
上面代码之中,只有一个this
,就是函数foo
的this
,所以t1
、t2
、t3
都输出同样的结果。因为所有的内层函数都是箭头函数,都没有自己的this
,它们的this
其实都是最外层foo
函数的this
。
除了this
,以下三个变量在箭头函数之中也是不存在的,指向外层函数的对应变量:arguments
、super
、new.target
。
function foo() { setTimeout(() => { console.log('args:', arguments); }, 100);}foo(2, 4, 6, 8)// args: [2, 4, 6, 8]
上面代码中,箭头函数内部的变量arguments
,其实是函数foo
的arguments
变量。
另外,由于箭头函数没有自己的this
,所以当然也就不能用call()
、apply()
、bind()
这些方法去改变this
的指向。
(function() { return [ (() => this.x).bind({ x: 'inner' })() ];}).call({ x: 'outer' });// ['outer']
上面代码中,箭头函数没有自己的this
,所以bind
方法无效,内部的this
指向外部的this
。
长期以来,JavaScript 语言的this
对象一直是一个令人头痛的问题,在对象方法中使用this
,必须非常小心。箭头函数”绑定”this
,很大程度上解决了这个困扰。
- 箭头函数与function的区别
- ES6---箭头函数与function定义函数有什么区别?在箭头函数中,this指向,构造函数,变量提升是如何表现的?
- es6的箭头函数,function* , yeild详解
- 箭头函数和非箭头函数的区别
- 箭头函数和普通函数的区别
- 损失函数(Loss function)和代价函数(成本函数)(Cost function)的区别与联系
- oracle ——function函数与procedure的区别
- Function 与 new Function 的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- jQuery(function(){})与(function(){})(jQuery)的区别
- 37、C#学习笔记3
- 如何正确使用debug
- express session 操作
- 打印十字图
- Drawable与 Bitmap 常用方法
- 箭头函数与function的区别
- Spring MVC配置文件的三个常用配置详解(annotation-config,annotation-config,annotation-driven)
- gSOAP简单多线程服务器程序
- Android音频跳动的View
- Hibernate学习(一)
- CentOS7中firewalld的安装与使用详解
- Python的替换函数——strip(),replace()和re.sub()
- http你不得不知道的那些事(一)--同源策略(2)
- angular路由