Function declaration and function expression in Javascript

Source: Internet
Author: User
Tags variable scope

JavascriptIt is a kind of object-oriented dynamic client scripting language that is prototyped and inherited from Netscape LiveScript. It mainly aims to solve server-side languages, such as Perl, the speed issues left over to provide customers with smoother browsing results.

Javascript has many interesting usage, which can be found in Google Code Search. For example:

 
 
  1. <script>  
  2. ~function() {  
  3. alert("hello, world.");  
  4. }();  
  5. </script> 

If you try this code, it means to declare a function and execute it immediately. Because the variable scope in Javascript is based on the function, this can avoid variable pollution, but here the bitwise operator "~" At first glance, people cannot understand it. If you remove it and run it again, an error will be reported: SyntaxError.

Before explaining why, let's clarify two concepts in Javascript: function declaration and function expression:

Let's take a look at the function declaration:

 
 
  1. <script>  
  2. function()   
  3. {  
  4. alert("hello, world.");  
  5. };  
  6. function foo()   
  7. {  
  8. alert("hello, world.");  
  9. };  
  10. </script> 

Let's take a look at what kind of function expressions are:

 
 
  1. <script>  
  2. var foo = function()   
  3. {  
  4. alert("hello, world.");  
  5. };  
  6. </script> 

Now let's look back at the question at the beginning of the article. Why do we remove the bitwise operator "~"? An error is reported during subsequent running. This is because Javascript cannot directly use parentheses after function declaration from the perspective of Syntax Parsing, but function expressions do not have this restriction, add "~" before the function declaration Operator, you can let the syntax parser regard it as a function expression. Similarly, add "!, +,-"And other operators are also feasible.

So why don't we use the following function expression?

 
 
  1. <script>  
  2. var foo = function()   
  3. {  
  4. alert("hello, world.");  
  5. }();  
  6. </script> 

Although there is no problem from the perspective of Syntax Parsing, the above Code has drawbacks. It introduces a variable, which may pollute the existing runtime environment and bring potential problems.

Use the bitwise operator "~" In fact, the function declaration is more readable with parentheses:

 
 
  1. <script>  
  2. (function()   
  3. {  
  4. alert("hello, world.");  
  5. })();  
  6. </script> 

After understanding the principle, no matter what writing method you encounter, you will not be confused by the second monk.

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.