箭头函数返回对象加()
//箭头函数返回对象的时候要加个()
var fnc = () => ({
name: 'zong',
age: 33
})
箭头函数没有this, arguments
**箭头函数没有this**
doSomethingLater: function() {
console.log(this)
//since the arrow function was created within the "obj"
//即箭头函数在哪里定义的,this就指向哪个作用域
// setTimeout(() => {
// this.count++
// console.log(this.count)
// }, 2000)
//但凡创建一个函数就会创建新的相应的作用域链,这里的回调函数是系统调用的,指向window
setTimeout(function() {
console.log(this)
this.count++
console.log(this.count)
}, 2000)
}
**没有绑定 arguments**
var argument = [1, 2, 3]
var arr = () => argument[0]
console.log(arr())
function foo(n) {
//console.log(arguments)
//这里arguments[0] 就是 n
//var f = (arg) => arg + n
var f = () => {
//箭头函数的arguments也是外层函数foo的
//console.log(arguments)
console.log(arguments[0],arguments[1],n)
return arguments[1] + n
}
//var f = () => argument[0] + n
return f(11)
// var f = (...args) => args[0] + n
// return f(6)
}
console.log(foo(3,8))
箭头函数不能用来new 对象
var Foo = () => {};
var foo = new Foo(); // TypeError: Foo is not a constructor
箭头函数不能使用原型链
var Foo = () => {};
console.log(Foo.prototype); // undefined
call/apply/bind不能用在箭头函数上
var obj = {
num: 100
}
// Setting "num" on window to show how it gets picked up.
window.num = 2020; // yikes!
// Arrow Function
var add = (a, b, c) => this.num + a + b + c;
// call
console.log(add.call(obj, 1, 2, 3)) // result 2026
// apply
const arr = [1, 2, 3]
console.log(add.apply(obj, arr)) // result 2026
// bind
const bound = add.bind(obj)
console.log(bound(1, 2, 3)) // result 2026
箭头函数解析顺序,要用()包装成对象使用
let callback;
callback = callback || function() {}; // ok
callback = callback || () => {};
// SyntaxError: invalid arrow-function arguments
callback = callback || (() => {}); // ok
本文由 dealdot <dealdot#163.com> 创作, Full Stack Developer @ DeepBlue
本文最后编辑时间为: Apr 17, 2021 at 17:34 pm
转载请注明来源