美文网首页
基本语法

基本语法

作者: Shaun_lan | 来源:发表于2017-06-19 17:11 被阅读1次

    1、匿名函数

    把一个函数作为另一个函数b的参数,参数函数就叫做匿名函数,因为不需要给函数起名字

    function b( someFun , value) { someFun(value); }

    b(function(key){console.log(key);}, 'value');

    3.实例

    function test(param, cb) {

    if (param == 'aaa') {

    //cb只有一个参数返回时,对应test匿名函数的error

    return cb('this is a error!');

    } else {

    return cb(null,param);

    }

    }

    var param = 'aaa';

    test(param, function(error, result){

    if (error) {

    console.log(error);

    //output:this is a error!

    } else {

    //if param ='aa'

    console.log(result);

    }

    });

    相关文章

      网友评论

          本文标题:基本语法

          本文链接:https://www.haomeiwen.com/subject/zkfnqxtx.html