The pre-loading of JavaScript from the definition of function

Source: Internet
Author: User

There are two ways to define a function in javascript:

function ftn () {}//first var ftn = function () {}///second type

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. Jinyun County Foreign Experts Bureau

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:

<script type= "Text/javascript" >function ftn () {alert (' old ');} var B = ftn;function ftn () {b (); Alert (' New ');} FTN ()//browser reported memory overflow </script>

Next I did a second test:

<script type= "Text/javascript" >var ftn = function () {alert (' old ');} var b = Ftn;var ftn = function () {b (); Alert (' New ');} FTN ();//old,new pop-up </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:

<script type= "Text/javascript" >function ftn () {alert (' old ');} var B = ftn;function ftn () {b (); Alert (' New ');} alert (b);//result is redefined FTN content </script>

Test results found that after redefining FTN, the content of B will change as well. Now I've done two more tests:

<script type= "Text/javascript" >var ftn = function () {alert (' old ');} var b = Ftn;var ftn = function () {b (); Alert (' New ');} alert (b);//result is old FTN content </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:

<script type= "Text/javascript" >function ftn () {alert (' old ');} var b = Ftn;var ftn = function () {b (); Alert (' New ');} alert (b);//Old Ftnalert (FTN);//New Ftnftn ();//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:

<script type= "Text/javascript" >//demo1function ftn () {    alert (' old ');} function ftn () {    B ();    Alert (' new ');} var B = ftn;ftn ();//Browser report memory overflow </script>
<script type= "Text/javascript" >//demo3function ftn () {  alert (' old ');} function ftn () {  B ();  Alert (' new ');} var B = Ftn;alert (b);//results are redefined FTN content </script>

Pre-loading is probably the way it is.

The pre-loading of JavaScript from the definition of function

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.