Js implementation is similar to add (1) (2) (3) method of calling
This article mainly introduces the js implementation method similar to add (1) (2) (3). If you need it, refer
The Code is as follows:
Var add = function (){
Return function (B ){
Return function (c ){
Return a + B + c;
};
};
};
Add (1) (2) (3); // 6
That's right! If there are four calls such as add (1) (2) (3) (4), this certainly does not apply.
This is similar to executing a function to return the function's own value:
The Code is as follows:
Function add (x ){
Var sum = x;
Var tmp = function (y ){
Sum = sum + y;
Return tmp;
};
Tmp. toString = function (){
Return sum;
};
Return tmp;
}
Console. log (add (1) (2) (3); // 6
Console. log (add (1) (2) (3) (4); // 10
However, after the computation is complete, the tmp function is returned, so that the computation result cannot be obtained. The result we need is a calculated number. What should we do first? In JavaScript, the toString or valueOf function is called for print and sum calculation respectively. Therefore, we rewrite the toString and valueOf methods of tmp and return the value of sum;
The above is all the content of this article. I hope you will like it.