function funcName(params) {
return params + 2;
}
funcName(2);
// 4var funcName = (params) => params + 2 funcName(2); // 4
() => { statements }parameters => { statements }parameters => expression
// 等价于:
function (parameters){
return expression;
}var double = num => num * 2
double(2); // 4 double(3); // 6
function Counter() {
this.num = 0;
}
var a = new Counter();console.log(a.num); // 0
function Counter() {
this.num = 0;
this.timer = setInterval(function add() {
this.num++;
console.log(this.num);
}, 1000);
}var b = new Counter(); // NaN // NaN // NaN // ..
clearInterval(b.timer);
function Counter() {
this.num = 0;
this.timer = setInterval(function add() {
console.log(this);
}, 1000);
}
var b = new Counter();clearInterval(b.timer);
function Counter() {
this.num = 0;
this.timer = setInterval(() => {
this.num++;
console.log(this.num);
}, 1000);
}
var b = new Counter();
// 1
// 2
// 3
// ...function Counter() {
var that = this;
this.timer = setInterval(() => {
console.log(this === that);
}, 1000);
}
var b = new Counter();
// true
// true
// ...clearInterval(b.timer);
箭头函数写代码拥有更加简洁的语法;
不会绑定this。
以上就是JavaScript箭头函数的用法介绍的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号