Analysis of Small

Source: Internet
Author: User

Two days ago a friend asked me a message about JS, in fact, before this friend has been asked this kind of question, after thinking think written out to those who are still answering this question in the friends.

Prototype

var add = function (m) {

var temp = function (n) {

Return Add (m + N);

}

temp.tostring = function () {

return m;

}

return temp;

};

Add (3) (4) (5); 12

Add (3) (6) (9) (25); 43

This add function can invoke the loop call indefinitely and add all the values passed in, and finally return the sum total. This problem is a bit special, but the code is extremely few but good, the focus of technology is: scope, alternating, anonymous function, tostring clever .

Let's explain the process: Add (3) (4) (5)

1, first executes the Add (3), at this time m=3, and returns the TEMP function;

2, Execute temp (4), this function executes the Add (M+n), n is the value of the passed in 4,m or 3 in the previous step, so Add (m+n) =add (3+4) =add (7), at this time m=7, and return the TEMP function

3, Execute temp (5), this function executes the Add (M+n), n is the value of the passed in 5,m or 7 in the previous step, so Add (m+n) =add (7+5) =add (12), at this time m=12, and return the TEMP function

4, the key step, there is no incoming parameter, is equal to the return of the temp function is not executed but printing, know JS friends know that the object's ToString is to modify the object conversion string method, so the code in the ToString function of the temp function return m value, The M-value is the value m=12 when the function was last executed, so the return value is 12.

See this is actually very clear, the code temp.tostring rewrite just for the function to return the result value of the last operation, so this place can be arbitrarily modified, you let it return what it returns, such as rewriting:

temp.tostring = function () {

Return "total:" + M;

}

Execution Result:

>>> Add (3) (4) (5);

Total:12

Extended

function Fun (n,o) {

Console.log (o)

return {

Fun:function (m) {

Return fun (m,n);

}

};

}

var a = fun (0);  A.fun (1);  A.fun (2); A.fun (3); undefined,0,0,0

var B = Fun (0). Fun (1). Fun (2). Fun (3); undefined,0,1,2

var c = Fun (0). Fun (1);  C.fun (2); C.fun (3); undefined,0,1,1

function fun, need two parameters N and O, first print out the parameter o, and then return an object, the object contains a method fun, method fun Call the function fun and return its value.

Explain the relevant process:

    • var a = fun (0);  A.fun (1);  A.fun (2); A.fun (3)

    • Do fun First (0), n=0,o=undefined,console.log print out undefined;

    • Re-execute A.fun (1), n=0,m=1,console.log print out 0;

    • Re-execute A.fun (2), n=0,m=2,console.log print out 0;

    • Re-execute A.fun (3), n=0,m=3,console.log print out 0;

    • var B = Fun (0). Fun (1). Fun (2). Fun (3);

    • Do fun First (0), n=0,o=undefined,console.log print out undefined;

    • Then do fun (1), n=0,m=1,console.log print out 0;

    • Then do fun (2), n=1,m=2,console.log print out 1;

    • Then do fun (3), n=2,m=3,console.log print out 2;

    • var c = Fun (0). Fun (1);  C.fun (2); C.fun (3);

    • Do fun First (0), n=0,o=undefined,console.log print out undefined;

    • Then do fun (1), n=0,m=1,console.log print out 0;

    • Re-execute C.fun (2), n=1,m=2,console.log print out 1;

    • Re-execute C.fun (3), n=1,m=3,console.log print out 1;

Transfer from: Original

Front-end JavaScript

Analysis of Small

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.