javascript data format (four) of the object basis functions ---

First, the definition of the function

  Functional block period can be reused

 

1.1 function declaration and function expression

  Defined functions commonly used in two ways as a function declaration and function expression, as follows:  

    function fn1 () {// function declarations
      the console.log ( 'Fn1 ()')
    }
    var Fn2 = function () {// expression
      the console.log ( 'Fn2 ()')
    }

About the difference between function declarations and function expressions, the god of reference: https://www.cnblogs.com/chaoyuehedy/p/9110063.html

 

1.2 function calls

  Function calls in the following ways:

  • Direct call: test ()
  • By calling the object: obj.test ()
  • new calling: new test ()
  • Let become interim test method of obj call: test.call/apply (obj)

 

1.3 callback function

  Simple to understand what function is a callback function?

  • You define
  • You do not tune
  • But in the end it is executed (at a certain time or a certain condition)

Common callback function:

  • dom event callback function ==> events dom element
  • Timer callback function ===> window
  • ajax request a callback function
  • Lifecycle callback function:

1.4 IIFE (immediately execute the function)

  Full name: Immediately-Invoked Function Expression, namely immediately execute the function. Commonly written as:

    (function () {// anonymous function call from
      var. 3 A =
      the console.log (A +. 3)
    }) ()

The above is understood as (anonymous function) (), i.e. directly after a defined function anonymous () to call the anonymous function. More knowledge about the immediate execution of the function, refer to: https://www.cnblogs.com/cnfxx/p/7337889.html

 

Perform the function of the left and right immediately:

  • Hidden achieve its internal variables are not visible outside (local function variables scope as a function of the body), is a variable safer
  • Does not pollute the external (global) namespace, the internal variables local variables, global variables will not cause changes
  • It is used to encode module js

 

1.5 Functions of this

What 1.5.1 this that?

  • Any function essentially invoked by an object, that is, if not directly specified window

  • All internal functions have a variable this
  • Its value is a function of the current object call

1.5.2 How to determine the value of this?  Remember the following points

  • test(): window
  • p.test(): p
  • new test (): newly created objects
  • p.call(obj): obj

  Learn more about JavaScript this, please refer to: https://www.cnblogs.com/yuanbo88/p/6290543.html

 

Guess you like

Origin www.cnblogs.com/hebing0415/p/11563617.html