Blogger Information
Blog 17
fans 0
comment 0
visits 12849
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
匿名函数、箭头函数和立即执行函数
再见羊肉串儿
Original
1034 people have browsed it

  • 匿名函数
    let sum = function (a,b) {
    return a + b;
    }

  • 箭头函数
    sum = (a,b) => {
    return a + b;
    }

    如果函数体只有一条语句,还可以简化为:
    sum = (a, b) => a + b;

只有匿名函数才可以转化为箭头函数,箭头函数不能当构造函数


一般情况下是先声明一个函数,然后再调用函数;

function sum(a, b) {
console.log(a + b);
}
sum(10, 20);

立即执行函数是把声明和调用二合一,一步完成;
(function sum(a, b) {
console.log(a + b);
})(10, 20);

  • 归并参数
    当不知道要传多少参数的时候可以用到归并参数
    sum = function (…arg){
    return arg;
    }
    console.log(sum(1,2,3,4));
    结果返回的就是一个数组[1,2,3,4];
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post