Tag: com ring uses targe demo1 x11 var to call its
There are two ways to define a function in javascript:
12 |
function ftn(){} // 第一种 var ftn = function (){} // 第二种 |
Some people say that these two kinds of writing are completely equivalent. Before parsing, however, the former is automatically promoted to the head of the code by the parser, so it violates the principle of first defining the function, so it is recommended to define the function in the latter way.
After reading this sentence, my first feeling is that the two are exactly the same at the time of use, but the resolution is different. But he explained that "the first way the parser was automatically promoted to the head of the code" confused me.
If I have the following first test:
12345678910111213141516 |
<script type=
"text/javascript"
>
function
ftn()
{
alert(
‘old‘
);
}
var
b = ftn; function
ftn()
{
b();
alert(
‘new‘
);
}
ftn();
//浏览器报内存溢出
</script>
|
Next I did a second test:
12345678910111213141516 |
<script type=
"text/javascript"
>
var
ftn =
function
()
{
alert(
‘old‘
);
}
var
b = ftn;
var ftn =
function
()
{
b();
alert(
‘new‘
);
}
ftn();
//old,new依次弹出
</script>
|
The explanation on the Internet is that the first way, in fact, does not redefine the function of FTN and executes itself in it. The second way, ftn=function () does not execute the code inside the function FTN has been redefined, so the redefinition here is valid.
If it's not clear, then I'll do one more test:
12345678910111213141516 |
<script type=
"text/javascript"
>
function
ftn()
{
alert(
‘old‘
);
}
var
b = ftn; function
ftn()
{
b();
alert(
‘new‘
);
}
alert(b);
//结果是重新定义的ftn内容
</script>
|
Test results found that after redefining FTN, the content of B will change as well. Now I've done two more tests:
12345678910111213141516 |
<script type=
"text/javascript"
>
var
ftn =
function
()
{
alert(
‘old‘
);
}
var
b = ftn; var
ftn =
function
()
{
b();
alert(
‘new‘
);
}
alert(b);
//结果是老的ftn内容
</script>
|
This is interesting, in JavaScript in addition to the basic data type, the other type is an object, the object is in the heap, its alias is the existence of the address of the stack, the latter kind of test can obviously be understood by this principle. So why did the previous test change with FTN's redefinition?
I have a new explanation, do not know right, in all of the JavaScript books will mention that there is no method of JavaScript in the overloaded, the definition of the name of the following function will overwrite the previous function,var B = ftn; The phrase is to point the reference B and ftn to the memory inside the same heap, and after redefining function ftn () {}, the new function object overrides the old object, and B and ftn refer to the heap address space unchanged, and if so, then this is reasonable:
123456789101112131415161718 |
<script type=
"text/javascript"
>
function
ftn()
{
alert(
‘old‘
);
}
var
b = ftn;
var
ftn =
function
()
{
b();
alert(
‘new‘
);
}
alert(b);
//老的ftn
alert(ftn);
//新的ftn
ftn();
//old ,new
</script>
|
So the new ftn in the stack address changes, pointing to the definition of a new function object, and the original function is not overwritten, but also saved, so B is still the old FTN reference address.
Just wrote a JavaScript inside the function understanding of the article, back to think about my side of the article content, feel that they pass the test results of understanding or a bit of a problem, in fact, understand or to be compiled from the principle of operation to think. JavaScript compiles code when code is executed, so we can define the type of Var, we encapsulate the object to add properties and methods, so I can understand the problem of the title, JavaScript general language, such as the definition of a variable var obj = New Object () is just a very early processing, in JavaScript is called pre-compilation, this kind of precompiled ability is very weak, weak to you can arbitrarily change, without affecting the operation of the program, when the object is called, the JavaScript interpreter will compile, and then run the code, This is very different from Java, Java is to compile the code first, call him to run, this is the scripting language features, so the scripting language is mostly unpleasant. But when you define a function like this: Fonction ftn () {}, this compiles the code, which is executed, which is similar to the definition of a Java function. This is my new solution, I think this explanation is more reasonable.
JavaScript's "Compile" just checks for code errors and does not run the code, so you can try writing something in the function to test it. Pre-loading, first function, then var. In the above code, only Demo1 and Demo3 are affected, demo1 and Demo3 source code order Function-var-function, applying pre-loaded order: Function-function-var, pre-loaded full code:
123456789101112131415 |
<script type=
"text/javascript"
>
//demo1
function
ftn()
{
alert(
‘old‘
);
}
function
ftn()
{
b();
alert(
‘new‘
);
}
var
b = ftn;
ftn();
//浏览器报内存溢出
</script>
|
123456789101112131415 |
<script type=
"text/javascript"
>
//demo3
function
ftn()
{
alert(
‘old‘
);
}
function
ftn()
{
b();
alert(
‘new‘
);
}
var b = ftn;
alert(b);
//结果是重新定义的ftn内容
</script>
|
Pre-loading is probably the way it is.
The pre-loading of JavaScript from the definition of function