First, function declaration and expression
function declaration: Functions Test () {};
Test (); Operating normally
function Test () {};
function expression: var test = function () {};
Test Undefined
Test (); TypeError
var test = function () {};
An assignment expression for a named function:
var test = function bar () {
Test (); Normal operation
};
Test (); Referenceerror
Second, this
1. Global scope, function call, method call, call constructor, display setting this;
Note: In strict mode, there is no global variable, this = undefined, in the object's literal declaration syntax, this cannot be used to point to the object itself;
The settings shown are this:
function Test (A, B, c) {};
var bar = {};
Foo.apply (Bar, [1, 2, 3]); The array will be expanded;
Foo.call (bar, 1, 2, 3); The parameters passed to test are: a = 1, b = 2,c = 3;
When using the call or Apply method on Function.prototype, the This within the function is displayed as the first parameter of the function invocation;
2. Common Misconceptions
1. Foo.method = function () {
function Test () {
This is a global variable;
};
Test ();
};
Foo.method = function () {
var = this;
function Test () {
That points to the Foo object;
};
Test ();
};
Iii. Closures and references
Closures: The current scope always has access to variables in the outer scope.
Cases:
function counter (start) {
var count = start;
return {
Increment:function () {
count++;
},
Get:function () {
return count;
}
};
};
var test = counter (4);
Test.increment ();
Test.get (); 5;
Closures in the loop:
for (var i = 0; i < i++) {
SetTimeout (function () {
Console.log (i); 10 of 10;
}, 1000);
};
Method:
1. for (var i = 0; i < i++) {
(function (e) {
SetTimeout (function () {
Console.log (e); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}, 1000);
}) (i);
};
2. for (var i = 0; i < i++) {
SetTimeout (function (e) {
return function () {
Console.log (e); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
} (i), 1000);
};
Iv. Arguments objects
Within each function, you can access a variable arguments, which maintains all incoming parameter lists, not an array, cannot use the pop (), push () array method, but can access its length;
Note: The arguments object is not created when arguments is declared as a local variable and as a formal parameter;
Note: It is strongly recommended not to use Arguments.callee;
Convert to array: Arrary.prototype.slice.call (arguments); But the conversion is relatively slow, not recommended;
Arguments creates getter and setter methods, changing the value of the parameter changes the value of the arguments object, and vice versa;
Cases:
function Test (A, B, c) {
Arguments[0] = 10;
Console.log (a); 10;
b = 20;
Console.log (Arguments[1]); 20;
var d = c;
D = 9;
Console.log (c); 3;
};
Test (1, 2, 3);
function Test (a) {
' Use strict ';
A = 10;
Console.log (A, arguments[0]); 10, 1
};
Test (1);
V. Transfer of parameters between functions
1. function foo () {
Test.apply (null, argument);
};
function (A, B, c) {};
2. Use both call and apply:
function Foo () {};
Foo.prototype.method = function (A, B, c) {
Console.log (This, a, b, c);
};
Foo.method = function () {
Foo.call.apply (Foo.prototype.method, arguments);
};
Or:
Foo.method = function () {
var args = Array.prototype.slice.call (arguments);
Foo.prototype.method.apply (Args[0], args.slice[1]);
};
JavaScript Learning Summary (three, function declarations and expressions, this, closures and references, arguments objects, passing parameters between functions)