Continue Uncle Tom's JS tour.
Secret named function expressions
function expressions and Function declarations
Uncle Tom quoted the ECMA specification in the blog: function declarations must have identifiers, and function expressions can be omitted. For me, the concept of these things really does not fit. Or an example of a big uncle. The example above is as follows:
function foo () {};//Ghost knows it's a statement.
var bar = function foo () {};//Ghost knows it's an expression.
New function Bar () {};
(function () {
function Bar () {};//This is also a declaration
}) (); These people should all look good to understand. Here the uncle also summed up a point can be in most cases of rapid judgment is an expression or function declaration "assignment must be an expression, without the function name must be an expression."
There is also a function expression that is less common and is enclosed in parentheses (function foo () {}). This is the reason for the expression. The simple parentheses () are a grouping operator, and more importantly, the inner part of the grouping operator can only contain expressions.
Function statements
Although this is mentioned, there may still be no support (the Chrome browser I see is supported) (craved I myself did not think well or fully understand).
Named function expressions
Directly on the instance var bar = function foo () {}; This is a valid named function expression, which the Uncle deliberately emphasizes is valid within the scope of the new definition function. Look at this example of the uncle, understand the understanding:
function foo () {return bar ();}
var bar = (function () {
if (Window.addeventlistener) {
return function bar () {
return Baz ();
};
}
else if (window.attachevent) {
return function bar () {
return Baz ();
};
}
})();
function Baz () {
Debugger
}
Foo ();
Let's take a look at these function call procedures:foo->bar; and bar is a named function expression that is returned by the internal two times after the Baz, that is, Baz is called by Bar. Bar->baz;baz Internal Call debugger.
JS Bug
- The identifier of the function expression is disclosed to an external scope; Example 1
- A named function expression is used as both a function declaration and a function expression; Example 2
- A named function expression creates two distinct function objects; Instance 3
- Simply parse the function declaration in order and ignore the conditional statement block; Example 4
Instance 1:var f = function g () {};typeof g;//"function"; tested successfully on IE8. This is not supposed to, the identifier G is parsed into a function, perhaps the front-end programmer in writing programs many times the bug is caused by this.
Instance 2:var typeof G;var F = function g (); Also under IE8 is function, under chrome is undefined
Instance 3:var f = function g () {};f = = = G;f.expando = ' foo '; g.expendo; test chrome cannot run (show G not defined).
Instance 4:var f = function g () {return 1;}; if (false) {F=function g () {return 2;};} g (); Test chrome is not defined under G.
So many questions, in fact, are the identifiers as a function of the declaration (personal understanding).
JS Memory Management
First look at the example given by the uncle, and then we understand the uncle to bring us the JS memory management of these contents. var f = (function () {if (true) {return function g () {};} return function g () {};}) ();。 The function returned by the anonymous function call (the function with the identifier g) is then assigned to the outer F; This object is not a thing with the returned function object, and the extra G function dies in the closure of the return function. This is why the memory problem occurs.
(A personal comment on this, when the function returned by the anonymous function is assigned to F, the bottom of the assignment is how to implement it)
The workaround is to manually disconnect the reference, as shown in the following example.
var f = (function () {var f,g;if (true) {F=function g () {};} Else{f=function g () {};} G=null; return f;}) ();
I read Uncle Tom's deep Understanding JS (ii)