Way to create a function of a small sum

1, the function declaration (static)
  
function static () {}

2, a function expression (function literal)
  var static = function () {}

. 3, function construction method, the parameters must be quoted (dynamic, anonymous)
  var static = new function ( 'n1' , 'n2');

the difference between the two, 2: parser will first read the function declaration, and make it accessible before executing any code; must wait until the function expression parse to perform it in the line of code will actually be interpreted.

Strictly speaking, self-executing function, also called function expression, which is mainly used to create a new scope, the variables declared within this role, not within the scope of variables and other conflict or confusion, is mostly the way anonymous function and automatic execution immediately.
(function (N1, N2) {
the console.log (N1 + N2)
}) (1,2); //. 3

a third type of function expression from a technical point of view, that is. This method is generally not recommended defined function, such as parsing syntax causes two codes (first parse routine ECMAScript code is the second parsing the incoming string constructor), which could affect performance.
Function () constructor will be resolved when each execution of the function body, and creates a new function object, so when you call a function in the Function loop or frequently executed () constructor efficiency is very low. The function literal is not every encounter will be re-compiled by Function () does not follow the typical scope when constructor creates a function that has it as a top-level function to perform.

This point

1, at a global scope: refers to the this window object;
2, in four cases the function of:
  1, the function declaration: indicates the this global scope, equivalent to window
  2, function expression (object method calls): refers to the current object
  3, the constructor: instance of an object point, that the newly created object
  4, use bind () / apply () / call () call: this means that the incoming first parameter

appreciated function context
// definition of an object
var context = {
text: "AAA",
UpDown: functiotn () {
the console.log (the this ); // print context
return This.Text;
}
}
// when this object by calling the function, the object is the article by context (context), this is the function value
context.updown ();
If the function is mounted on an object as a property of the object, called him the method of the object. When this object by calling the function, the object is the calling context (context), this is the value of the function. 

/ *************************************** /
based object: context (context)> this > variables and functions to mount the object
given function: scope> decision function definition> functions working

Guess you like

Origin www.cnblogs.com/Angxf/p/10966455.html