我经常的使用箭头函数,却尚未对箭头函数有个深刻的了解,如今找一下这2个函数的不一样点函数
因为箭头函数没有原型,所以箭头函数自己没有thisthis
let a = () => {} console.log(a.prototype) // undefined let b = function () {} console.log(b.prototype) // Object
let a; let barObj = { msg: 'bar的this指向' } let fooObj = { msg: 'foo的this指向' } bar.call(barObj) foo.call(fooObj) // { msg: 'bar的this指向' } bar.call(fooObj) a() // { msg: 'foo的this指向' } function foo() { a() } function bar () { a = () => { console.log(this) } }
从上面例子中能够得出2点:prototype
let b = () => { console.log(arguments); } b(1,2,3,4) // arguments is not defined function bar () { console.log(arguments); // 完成第二个普通函数 bb('完成第一个普通函数') function bb() { console.log(arguments); // 完成第一个普通函数 let a = () => { console.log(arguments); // 完成第一个普通函数 } a('箭头函数') } } bar('完成第二个普通函数')
从上面能够得出如下2点code
不管箭头函数的this指向哪里,使用new调用箭头函数都会报错,箭头函数没有构造函数继承
let a = () => {} let b = new a() // a is not a constructor