Experiment on the parsing and execution sequence of javascript

Source: Internet
Author: User


Javascript is an interpreted language and its execution is top-down. However, browsers have a slightly different understanding of [Top-Down], and the upstream and downstream of the code, that is, the Program Stream, is crucial to the proper running of the program. Therefore, it is necessary to thoroughly understand the execution sequence of js. For this reason, I have designed the following eight experiments to obtain the most accurate results.

Bin (to write to me) original blog (http://blog.csdn.net/binbinxyz), reprint please indicate the source!

<Script type = "text/javascript"> // Experiment 1: function t (a) {alert ("[t (a)] a:" + );} function t (a, B) {alert ("[t (a, B)] a:" + a + ", B:" + B) ;}t (1 ); // result: // [t (a, B)] a: 1, B: undefined // Experiment 2: function t (a, B) {alert ("[t (a, B)] a:" + a + ", B:" + B);} function t () {alert ("[t (a)] a:" + a) ;}t (1); // result: // [t (a)]: 1 // experiment 3: function t (a) {alert ("[t (a)] a:" + a);} function t (a, B) {alert ("[t (a, B)] a:" + a + ", B:" + B) ;}t (1, 2); // result: // [t (a, B)] a: 1, B: 2 // Experiment 4: function t (a, B) {alert ("[t (a, B, b)] a: "+ a +", B: "+ B);} function t (a) {alert (" [t (a)]: "+ a);} t (1, 2); // result: // [t (a)] a: 1 // Experiment 5 function t () {alert ("[t (a)] a:" + a) ;}t (1); function t (a, B) {alert ("[t (, b)] a: "+ a +", B: "+ B);} // result: // [t (a, B)] a: 1, B: undefined // Experiment 6 function t (a) {alert ("[t (a)] a:" + a) ;}t (1, 2); function t (, b) {alert ("[t (a, B)] a:" + a + ", B:" + B) ;}// result: // [t (a, B)] a: 1, B: 2 // experiment 7 function t (a, B) {alert ("[t (a, B)] a: "+ a +", B: "+ B);} t (1); function t (a) {alert (" [t (a)]: "+ a);} // result: // [t (a)] a: 1 // experiment 8 function t (a, B) {alert ("[t (a, B)] a:" + a + ", B:" + B);} t (1, 2); function t () {alert ("[t (a)] a:" + a) ;}// result: // [t (a)] a: 1 </script>

When defining a javascript function, the function name is the identifier of the function object. The number of parameters is only the attribute of the function. It is not feasible to implement overload by defining functions with different numbers of parameters.
When calling a function, js finds the corresponding function object through the function name, and matches the parameter list with the expression parameter list in order according to the parameter defined in the function, removing unnecessary parameters, parameters that are not enough are processed by undefined, and then the function code is executed.

Therefore, when defining a function, the required parameters are usually placed at the beginning of the parameter list, and the optional parameters are placed after the required parameters.

NOTE 1: The results of the above eight experiments are obtained through the 360 browser (version/kernel: and Firefox browser (Version: 27.0.1.
2. The above eight experiments are independent of each other. Run them separately to obtain the correct results.

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.