箭头函数的特点

2 篇文章 0 订阅

没有自己的this

  • 箭头函数中的this实际是外层函数的this
const test={pro:'pro',con:()=>console.log(this)}
test.con() //window
const foo={bar:{con:test1.con},pro:'foopro'}
foo.bar.con() //window(this与调用层数没有关系this是函数才有的内部变量,所以箭头函数的this总是来自外层函数中的this,这里指向window)
foo2 = {bar:{con:function(){console.log('function',this)}},pro:'foopro'}
foo2.bar.con() //function {con: ƒ} (指向函数的调用者bar)
  • 箭头函数不能当作构造函数使用
  • 不能用call()、apply()、bind()这些方法去改变this的指向

不存在arguments对象

该对象在函数体内不存在。如果要用,可以用 rest 参数代替。
rest形式为(…变量名)可以获取函数的多余参数,并且获取到的是真正的数组对象
arguments只是一个类数组对象,除了length属性和可以使用arguments[0]获取元素之外没有数组的其他特性,可以使用[…arguments]或者Array.prototype.slice.call(arguments,0)、Array.prototype.concat.call(arguments,0)转为数组

没有prototype属性

var foo = () => {  
    return 1  
}  
function bar(){  
    return 2;  
}  
console.log(foo.prototype)  //undefined  
console.log(bar.prototype)  // {constructor:f}

不可以使用yield命令

因此箭头函数不能用作 Generator 函数。
这个还不知道为什么

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值