Introduction
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!
Lab
<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: 6.3.1.142/21.0.1180.89) 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.