JavaScript Advanced (6)---function declaration

Source: Internet
Author: User

-------------

    • The function in JS is also an object
    • The return value of the function is determined by return, and no return undefined

1. Function declaration method

      1.  function   add (A, b) {A  = a + A;     b  = b + b;}  
        2, function expression method
      2.  var  add = function  (A, b) {...}; 
      3.  (function  () {...}
           ());
      4.  return  function  () {...} 
      5.  var  add =function  foo (A, b) { ......}; //
           named function expression, there is a compatibility problem and is not commonly used  
        • Why can I call this function ahead of time in front of a function declaration?
          • Because function declarations are pre-processed at load time, in function expressions, only variables declared in the function are pre-processed.
          • Add (5,4); var add =function foo (A, b) {alert (a+b)};
          • And the function expression itself does not. Therefore, calling the function before the function expression will result in an error.
          • (I didn't think of chestnuts at the moment)

        3. Function constructor

        • var New Function (' A ', ' B ', ' Console.log (a+b); ' ) func (4,5);

     4. Arrow function

      • A new function has been added under the ES 6 standard: arrow function
      • For example:
        x = x*// equivalent to function  (x) {    return x*x;  }

      • Arrows are the equivalent of omitting the key words "function", "{}", "Return"
      • Special changes are required in the following special circumstances
          1. The argument is not a case
            // two parameters ()= $;       // No Parameters // variable-parameter conditions (x, Y, ... rest) = {    return x+y;  }
          2. The returned content is the case of the object
            x = {foo:x}     // error notation  = ({foo:x})   // correct wording
        • The arrow function completely fixes the point of the This keyword, in the arrow function, this always points to the lexical scope, that is, the outer caller
           var  obj_arrow = {birth:  1990, Getage:  function   () { var  b = this  .birth;  var  fn = () =>new  Date ().    getFullYear ()-this  .birth;   return   FN ();  }}console.log (Obj_arrow.getage ());  // 26 here directly points to obj  
          The arrow function uses apply with call, because its internal this is fixed to this method scope, then call and apply The first parameter passed in is ignored     

 

JavaScript Advanced (6)---function declaration

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.