Javascript Note: differences between different functions in Javascript

Source: Internet
Author: User

See todayJavascriptA summary of the experience is as follows:

InJavascriptDefine a function in two ways:

The first is:Function FTN (){}

The other is: Var FTN = function (){}.

The two statements are equivalent. But before parsing,The previous write method will be automatically upgraded by the parserCodeHeaderTherefore, it violates the principle of defining and then using a function. Therefore, we recommend that you use the last method when defining a function.

After reading this sentence, I first felt that the two statements were completely consistent during use, but they were resolved differently. However, his explanation "the previous writing method will be automatically promoted to the Code header by the parser" left me confused.

If I have the following first test:

 1     Function  FTN ()
2 {
3 Alert ( ' Old ' );
4 }
5
6 VaR B = FTN;
7
8 Function FTN ()
9 {
10 B ();
11 Alert ( ' New ' );
12 }
13
14 FTN (); // Browser reports memory overflow

Next I did the second test:

 1   VaR  FTN  =     Function  ()
2 {
3 Alert ( ' Old ' );
4 }
5
6 VaR B = FTN;
7
8 VaR FTN = Function ()
9 {
10 B ();
11 Alert ( ' New ' );
12 }
13
14 FTN (); // Old and new pop-up

The explanation on the internet is as follows: the first method was not redefined at the beginning.FTNThisFunctionAnd execute itself in it. Method 2,FTN = function ()Not executed hereFunctionCodeFTNThe definition has been redefined, so the definition here is valid.

However, this explanation makes me feel confused, so I did the following test:

 1   Function  FTN ()
2 {
3 Alert ( ' Old ' );
4 }
5
6 VaR B = FTN;
7
8 Function FTN ()
9 {
10 B ();
11 Alert ( ' New ' );
12 }
13
14 Alert (B ); // The result is the redefined FTN content.

Test results found, redefinedFTNAfter,BThe content will also change.

Next I did another two tests:

 1   VaR  FTN  =     Function  ()
2 {
3 Alert ( ' Old ' );
4 }
5
6 VaR B = FTN;
7
8 VaR FTN = Function ()
9 {
10 B ();
11 Alert ( ' New ' );
12 }
13
14 Alert (B ); // The result is the old FTN content.

This is very interesting.JavascriptExcept for the basic data type, all other types are objects, objects exist in the heap, and their aliases are addresses in the stack, this principle can be clearly understood in the next test. Why is the previous testBWill followFTNIs the definition changed?

I have a new explanation, right?JavascriptAs mentioned in the book,JavascriptThere is no method overload in it, and the duplicate name defined laterFunctionWill overwrite the previousFunction,VaR B = FTN;This sentence refersBAndFTNThe reference points to the memory in the same heap, And the definition is redefined.Function FTN (){}After, the newFunctionThe object overwrites the old object, whileBAndFTNThe referenced heap address space hasn't changed. If so, this method is reasonable:

 1   Function  FTN ()
2 {
3 Alert ( ' Old ' );
4 }
5
6 VaR B = FTN;
7
8 VaR FTN = Function ()
9 {
10 B ();
11 Alert ( ' New ' );
12 }
13
14 Alert (B ); // Old FTN
15 Alert (FTN ); // New FTN
16 FTN (); // Old, new

This newFTNThe address in the stack has changed, pointing to the newFunctionObject definition, and the originalFunctionNot overwritten and saved, soBOldFTNThe referenced address.

I just wrote an article about functions in JavaScript.ArticleLater, I thought about the content of my article. I think it is still a bit difficult to understand the test results. In fact, I still need to think about the principles of compiling and running. Javascript is compiled only when code is executed. Therefore, the types defined by VAR can be variable. When encapsulated objects are added, attributes and methods are added, therefore, we can understand the problems caused by my title. The general JavaScript language, for example, defining a variable var obj = new object (), is just a very initial process, it is called pre-compilation in Javascript. This pre-compilation capability is weak, so you can change it without affecting it.ProgramWhen the object is called, The javascript interpreter will compile and then run the Code. This is very different from Java, where the code is compiled first, it runs only when it is called. This is a feature of the script language, so most of the script languages are not very good. But when you define a function like this: fonction FTN () {}, the code is compiled, that is, executed. This writing method is similar to the definition of Java functions. This is my new explanation. I think this explanation is more reasonable.




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.