The order in which javascript is parsed varies from browser to browser
- 2020-03-30 02:22:40
- OfStack
Introduction to the
Javascript is an interpreted language that is executed from the top down. But browsers' understanding of top-down is nuanced, and the upstream and downstream, or flow, of the code is crucial for the program to work correctly. Therefore, it is necessary to have a deep understanding of the execution order of js. To this end, I designed the following eight experiments to obtain the most accurate results.
The experiment
Afterword.
When defining a javascript function, the name of the function is the identity of the function object, and the number of arguments is just an attribute of the function. It is not possible to overload a function by defining a different number of arguments.
When calling a function, js finds the corresponding function object by the function name, and then matches it in order according to the parameters defined by the function and the list of parameters of the expression.
So when you define a function, you usually put the required arguments at the top of the list and the optional arguments at the bottom.
Matters needing attention
I. the results of the above eight experiments were obtained by running 360 browser (version/kernel: 6.3.1.142/21.0.1180.89) and firefox browser (version: 27.0.1).
The above eight experiments are independent of each other. Please run them separately to get correct results.
Javascript is an interpreted language that is executed from the top down. But browsers' understanding of top-down is nuanced, and the upstream and downstream, or flow, of the code is crucial for the program to work correctly. Therefore, it is necessary to have a deep understanding of the execution order of js. To this end, I designed the following eight experiments to obtain the most accurate results.
The experiment
<script type="text/javascript">
//Experiment 1:
function t(a)
{
alert("[t(a)]a:" + a);
}
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
t(1);
//Results:
//[t(a, b)]a:1, b:undefined
//Experiment 2:
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
function t(a)
{
alert("[t(a)]a:" + a);
}
t(1);
//Results:
//[t(a)]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);
//Results:
//[t(a, b)]a:1, b:2
//Experiment 4:
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
function t(a)
{
alert("[t(a)]a:" + a);
}
t(1, 2);
//Results:
//[t(a)]a:1
//The experiment of five
function t(a)
{
alert("[t(a)]a:" + a);
}
t(1);
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
//Results:
//[t(a, b)]a:1, b:undefined
//The experiment of six
function t(a)
{
alert("[t(a)]a:" + a);
}
t(1, 2);
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
//Results:
//[t(a, b)]a:1, b:2
//The experiment of seven
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
t(1);
function t(a)
{
alert("[t(a)]a:" + a);
}
//Results:
//[t(a)]a:1
//The experiment of eight
function t(a, b)
{
alert("[t(a, b)]a:" + a + ", b:" + b);
}
t(1, 2);
function t(a)
{
alert("[t(a)]a:" + a);
}
//Results:
//[t(a)]a:1
</script>
Afterword.
When defining a javascript function, the name of the function is the identity of the function object, and the number of arguments is just an attribute of the function. It is not possible to overload a function by defining a different number of arguments.
When calling a function, js finds the corresponding function object by the function name, and then matches it in order according to the parameters defined by the function and the list of parameters of the expression.
So when you define a function, you usually put the required arguments at the top of the list and the optional arguments at the bottom.
Matters needing attention
I. the results of the above eight experiments were obtained by running 360 browser (version/kernel: 6.3.1.142/21.0.1180.89) and firefox browser (version: 27.0.1).
The above eight experiments are independent of each other. Please run them separately to get correct results.