我怎样才能替换arguments.callee;在javascript中

时间:2015-11-20 07:10:15

标签: javascript

这是我的代码

function add(x) {
    return function (y) {
        if (typeof y !== 'undefined') {
            x = x + y;
            return arguments.callee;
        } else {
            return x;
        }
    };
}
add(1)(2)(3)(4)();

1 个答案:

答案 0 :(得分:1)

作为替换 - 给它一个名字?:

function add(x) {
    return function my_func(y) {
        //          ^ Named function
        if (typeof y !== 'undefined') {
            x = x + y;
            return my_func;
        } else {
            return x;
        }
    };
}
add(1)(2)(3)(4)();

另请阅读:

  

请勿使用arguments.calleehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee