1
Function.prototype.times = function(val){
        var that = this;
        return function (arg) {
            if(val == 0){
                return that.call(this,arg);
            }
            else{
                var newArg = that.call(this,arg);
                var newVal = val - 1;
                return that.call(that.times,newVal,newArg);
            }
        }
    }
    var add_two = function (num) { return num + 2; };
    var add_six = add_two.times(3);
    alert(add_six(2));

有人可以帮我解释为什么这段代码不起作用吗?谢谢!

4

2 回答 2

0

有几个问题,这是一个固定的版本:

Function.prototype.times = function(val){
    var that = this;
    return function (arg) {
        if(val == 1) {
            return that.call(this,arg);
        } else{
            var newArg = that.call(this,arg);
            var newVal = val - 1;
            return that.times.call(that,newVal)(newArg);
        }
    }
}
var add_two = function (num) { return num + 2; };
var add_six = add_two.times(3);
alert(add_six(2)); // 8
于 2013-10-12T16:21:32.920 回答
0

请检查一下。 关联

基本上 add_six 不是一个函数,而是一个函数的引用。检查下面的代码

 alert(typeof add_six(2));
于 2013-10-12T16:04:01.333 回答