JavaScript 演练(4):回调函数


var func = function (callback, a, b, c) {
    return callback(a, b, c);
}

var funAdd = function (a, b, c) {
    function callback(a, b, c) { return a + b + c; }
    return func(callback, a, b, c);
}

var funMul = function (a, b, c) {
    function callback(a, b, c) { return a * b * c; }
    return func(callback, a, b, c);
}

var x = 2, y = 3, z = 4;
alert(funAdd(x, y, z)); //9
alert(funMul(x, y, z)); //24

alert(func(funAdd, x, y, z)); //9
alert(func(funMul, x, y, z)); //24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Grow your business fast with

Suku