首页 > 代码库 > 小技巧

小技巧

//toString机制function add(number1){    var sum = number1;    var app = function(number2){        sum += number2;        return app;    };    app.toString = function(){        return sum;    }    return app;}alert(add(1)); // 1console.log(add(1)(2)); // 3console.log(add(1)(2)(3)); // 6console.log(add(1)(2)(3)(4)); // 10
 //柯里化函数function sum(x){    function f(y){        return sum(x+y);    }    f.toString = f.valueOf = function(){        return x;    };    return f;}sum(1)(2)(3)alert( (1) )
//不用循环打印    function print(str,number){            var arr = new Array( (number || 0) + 1 );            var str = arr.join(str);            alert(str)        }        print("helloword ",10);

 

小技巧