Discuss / JavaScript / 不懂的一个点

不懂的一个点

Topic source

斑马茶爷

#1 Created at ... [Delete] [Delete and Lock User]
function make_pow(n) {
    return function (x) {
        return Math.pow(x, n);
    }
}

// 创建两个新函数:
var pow2 = make_pow(2);
var pow3 = make_pow(3);

pow2(5); // 25
pow3(7); // 343

不太理解这段——

pow2(5);//这个传入的5,不会传给n吗?能多解释一点么T T

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

这是函数定义:

function make_pow(n) {
    return function (x) {
        return Math.pow(x, n);
    }
}

执行:

var pow2 = make_pow(2)

相当于把2传给n:

function make_pow(2) {
    return function (x) {
        return Math.pow(x, 2);
    }
}

返回的结果是:

return function (x) {
    return Math.pow(x, 2);
}

相当于:

var pow2 = function (x) {
    return Math.pow(x, 2);
}

  • 1

Reply