Js implementation is similar to add (1) (2) (3) method of calling

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.