Call function Now

Source: Internet
Author: User

  • It is a good practice to immediately precede a function with a semicolon, like this (function () {alert (1)}) functions, if JS does not wrap, or is compressed, it is easy to make the contextual overlap confusion.
  • There are two ways to call a function, called through a function name, and called through an expression . Anonymous function calls are counted in the method called by the expression.
  • and "function f () {} (1)" is legal,
    This statement can be interpreted as two parts, the first part is function f () {}, the second part is (1) the expression, so the function is not called, the result is
    function F () {alert ("FFF")} (1)//1
  • "function f () {}" is approximately functiondeclaration,
    and "(1)" can be attributed to expression, as a parenthesis operator, or as a grouping operator.
  • A function declaration must have a functional name
  • Constructor and assign a value

    var abc = new Function ("x","y","return x*y;") );
    alert (ABC (2,3)); //"6"

    Alert ((function (x, y) {return x+y;}) (2,3)); //"5"
    Alert ((new Function ("x","y","return x*y;") )) (2,3)); //"6"

  • Parentheses can combine expressions into chunks, and each piece, that is, each pair of parentheses, has a return value. This return value is actually the return value of the expression in parentheses. So, when we enclose the anonymous function with a pair of parentheses, the parentheses are actually returned, which is the function object of one of the anonymous functions. Therefore, the parenthesis pair plus the anonymous function is like the name of the function that we get its reference position. So if you add a parameter list after the reference variable, the invocation form of the normal function is implemented.

  • (function () {alert (1)}) () should be equivalent to A=function () {alert (1)} () and not even a=.
  • So if you ask the jquery code snippet in the opening, what features are used in JS? Then it's just an anonymous function and an anonymous function call . However, it implies closure characteristics and can be implemented at any time. Because JS is born with this feature!

  • Closures can cause memory leaks, closures cause internal variables to be referenced outside, not released

  • When using strict mode, it is recommended that you turn on strict mode by a function (at least in the transition period of learning)
  •  function   Strict () { //  function-level strict-mode syntax  ' use  Strict ' ;  function  nested () {return  "and so Am i! "   return  "hi!  I ' m a strict mode function! "+ nested ();}  function  notstrict () {return  "I m not Strict. ";}  

     

  •  void   function  () {alert (2)} // undefined  void  function  () {alert (2)}(); You can call  new  function  () {alert (2< Span style= "color: #000000;" )} (); You can call  new  function  () {alert (2  

    &NBSP;

  • var function var return function return id++ }}, call mode NextID (); some functions are usually executed only once, such as for initialization. There's no need to waste time on it. A function name that can be reused var dd= function() {alert ("Hello, World"). ); } (); Parentheses are appended to the expression, which can be called



  • Now, whether you define a function like this function foo(){} or var foo = function(){} , when called, you need to add a pair of parentheses to the back, like this foo() .
  •  foo relative to function expression '  var  foo = function  () { /*   code  */   function  () {  code  */} (); // syntaxerror:unexpected token (  it treats it as a function without a name and throws an error because the declaration of functions requires a name. 
    varFoo =function() {Console.log (1)} (), the answer is yes. //However, the function declaration is syntactically invalid, it is still a declaration, and the enclosing parenthesis is invalid because the parentheses need to contain an expression.
    If you want to call an anonymous function directly, you need to let JS parse it according to the expression.functionFoo () {/*Code*/}();//syntaxerror:unexpected Token//now, you put an expression in parentheses without throwing an error ..., but the function is not executed because:functionFoo () {/*Code*/} (1)//it is equivalent to the following, a function declaration followed by an expression that is completely non-relational:functionFoo () {/*Code*/}(1);
    when parentheses wrap a function, it parses the function as an expression by default, not the function declaration. (function () {/* code */} ()); // Crockford recommends this one, the expression inside the parentheses represents the function call expression immediately (function () {/* code */}) (); // but this one works just as well, the expression in parentheses represents the function expression
  • The new function () {alert ("GG")} will call

  • One of the advantages of calling functions immediately is that when this is a very long function expression, it saves you time in reading your code, without having to scroll to the bottom of the page to see if the function is called.
  • as long as the JavaScript interpreter can handle the immediate execution of an anonymous function with a "function expression" rather than a "function declaration ," It's () just one way to put the statement in.

  • // 如果本身就是expression,那么根本不需要做任何处理var i = function(){ return 10; }();true && function(){ /* code */ }();0, function(){ /* code */ }();// 如果你不在乎返回值,可以这么做!function(){ /* code */ }();~function(){ /* code */ }();-function(){ /* code */ }();+function(){ /* code */ }();

    When parentheses appear at the end of an anonymous function to invoke a function, it defaults to the function declaration.

    When parentheses wrap a function, it parses the function as an expression by default, not the function declaration.

Call function Now

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.