JavaScript-箭头函数

箭头函数兼容性

箭头函数兼容性

箭头函数定义

lambda 函数以尽量简洁的语法定义函数,是函数表达式的简化版。

1
2
3
4
5
6
7
param => expression // 只有一个参数,省略括号,操作符,胖箭头 => 箭头函数的核心。仅一行语句,即为返回值。省略 { return expression }
() => expression // 没有参数,必须使用 () 是必须的,其它同上
(param1, param2) => expression // 一个以上参数,参数由括号包裹,参数以逗号隔开,其它同上
() => {
expression1;
expression2;
} // {} 包裹的函数体代码块,没有 return 语句,默认返回 undefined,否则返回 return 语句的值

箭头函数与函数执行上下文

箭头函数没有单独的 this箭头函数的 this 与 函数声明所在的上下文相同由以 demo 可知,因 箭头函数在 window 中定义,所以 this 指向 window,函数表达式 this 指向调用的环境,所以 this 指向 lambda 对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function thisLambda() {
let lambda = {
// 在 window 中声明
getThislambda: () => {
// assets('assets', true, this)
console.log('lambda', this)
},

getThis: function() {
console.log('function expression',this)
}
}

lambda.getThislambda() //Window {window: Window, self: Window, document: document, name: '', location: Location, …}
lambda.getThis() // {getThislambda: ƒ, getThis: ƒ}
}