JS implementation is similar to the Add (1) (2) (3) method of calling methods _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

var add = function (a) {
return function (b) {
return function (c) {
return a+b+c;
};
};
};
Add (1) (2) (3); 6

That's right! If the Add (1) (2) (3) (4) has such 4 calls, then this certainly does not apply.

This is analogous to performing a function return function itself value:

Copy Code code 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

But after the completion of the calculation or return the function of TMP, so that we can not get the result of the calculation, we need the result is a calculated number how to do it, first of all to know in JavaScript, print and add calculation, will call the ToString or valueof function, So we rewrite the ToString and valueof methods of TMP to return the value of sum;

The above is the entire content of this article, I hope you can enjoy.

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.