JS immediately called function expression how to write

Source: Internet
Author: User
Tags define function

  If you do not need to display the call function, let this function in the definition of the execution, how to write it can be, the following for you to introduce the specific implementation steps, like friends can understand the next

If you do not need to display the call function, let this function in the definition of the execution, how to write it can be, next will be detailed implementation steps, interested friends can understand under     1. preface   function needs to be defined first and then used. This is basically an iron law of all programming languages.   In general, we need to invoke a JavaScript function, and the basic situation is defined first and then called again. Look at an example   code as follows:  code as follows: <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  <html >  <head>  <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">  <title>say hello</title>  </head>  <body>  < script> //define function  function SayHello ()   {  alert ("Hello"); }  function  SayHello ();  </script>  </body>  </html>    But if you don't need to display the calling function, How do you write this function when it is defined?     2. The process of thinking   from the above examples, smart you combined with the above situation may want to:  = = "Since the call is in the function name after a pair of functions Is the definition followed by a pair of curly braces that can be executed? :  code like the following:  code as follows: function SayHello ()   {  alert ("Hello"); } ();   Unfortunately, the above wording will quote JS syntax errors.   Because the JavaScript parser resolves the global function or function intrinsic function keyword in the parser, the braces are parsed into function declarations instead of function expressions by default.     That is, the last pair of curly braces is parsed by default to a function with a missing name, and a syntax error message is thrown because the function declaration requires a name.     = = You might think again, if I passed the argument in curly braces, would it parse into an expression?   Code below:  code as follows: function SayHello ()   {  alert ("Hello"); } (1);    Indeed, the error is gone. But the above wording is equivalent to the following effect   code as follows: function SayHello ()   {  alert ("Hello"); };  (1);  & nbsp The two sentences have nothing to do with the function still won't execute     3. Correct writing   for JavaScript, parentheses () cannot contain statements, so at this point the parser is parsing the function keyword The corresponding code is parsed into a function expression, not a function declaration, so just enclose the braces in the code (including the part of the function plus a pair of curly braces).   Code below:    code as follows: (function SayHello ()   {  alert ("Hello"); } ());    There is also a way to do it, is to move the curly braces out of the back, as  code as follows:    code as follows: (function SayHello ()   {  alert ("Hello"); }) ();  & nbsp Recommendation is to use the first method.   But at present a lot of better JS library use is the second way.   For example: Web Graphics Drawing:git, draw2d,....  

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.