Function expressions are still very common in practical applications. In web development, a common pattern is based on a certain feature.
Function expressions are still very common in practical applications. In web development, a common pattern is to disguise function definitions based on tests on certain features to achieve performance optimization, however, because this method is in the same scope, function expressions must be used:
// The Code comes from the APE Javascript library (http://dhtmlkitchen.com/ape/) var contains = (function () {var docEl = document.doc umentElement; if (typeof docEl. compareDocumentPosition! = 'Undefined') {return function (el, B) {return (el. compareDocumentPosition (B) & 16 )! = 0 ;};} else if (typeof docEl. contains! = 'Undefined') {return function (el, B) {return el! = B & el. contains (B) ;}return function (el, B) {if (el = B) return false; while (el! = B & (B = B. parentNode )! = Null); return el = B ;};})();
When it comes to the naming function expression, it must have a name. In the preceding example, var bar = function foo () {}; is a valid naming function expression, but remember one thing: this name is valid only in the scope of the newly defined function, because the specification stipulates that the identifier cannot be valid in the peripheral scope:
Var f = function foo () {return typeof foo; // foo is valid within the internal scope}; // foo is used externally as invisible typeof foo; // "undefined" f (); // "function"
In this case, what is the use of the name function expression? Why?
As we said at the beginning: to give it a name is to make the debugging process more convenient, because during debugging, if each item in the call Stack has its own name, the debugging process is so cool that you may feel different.
Additional reading
The topic list of this article is as follows:
- How should we understand the working principle of the JavaScript engine?
- JavaScript exploration: the importance of writing maintainable code
- JavaScript exploration: exercise caution when using global variables
- JavaScript exploration: var pre-parsing and side effects
- JavaScript exploration: for Loop (for Loops)
- JavaScript exploration: for-in loop (for-in Loops)
- Exploring JavaScript: Prototypes is too powerful
- JavaScript: eval () is the devil"
- JavaScript exploration: Using parseInt () for Numerical Conversion
- Exploring JavaScript: Basic coding specifications
- JavaScript exploration: function declaration and function expression
- JavaScript exploration: Name function expressions
- JavaScript: function name in the debugger
- JavaScript: JScript Bug
- JavaScript exploration: Memory Management of JScript
- Exploring JavaScript: SpiderMonkey's quirks
- JavaScript exploration: an alternative solution to naming function expressions
- JavaScript exploration: Object
- JavaScript exploration: Prototype chain
- JavaScript exploration: Constructor
- JavaScript probing: executable context Stack
- Execution context 1: Variable object and activity object
- Execution context 2: Scope chain Scope Chains
- Execution context 3: Closure Closures
- Execution context 4: This pointer
- Exploring JavaScript: Powerful prototype and prototype chain
- JavaScript Functions 1: function declaration
- JavaScript function 2: function expressions
- JavaScript function 3: function expressions in a group
- JavaScript function 4: function Constructor
- JavaScript variable object 1: VO Declaration
- JavaScript variable object 2: VO in different execution contexts
- JavaScript variable object 3: two stages of execution Context
- JavaScript variable object IV: Variables
- Property of the JavaScript variable object __parent _
- JavaScript scope chain 1: Scope chain Definition
- JavaScript scope chain 2: function Lifecycle
- JavaScript scope chain 3: Scope chain features
- JavaScript closure 1: Introduction to closures
- JavaScript closure 2: Implementation of closure
- JavaScript closure 3: Closure usage
This article is available at http://www.nowamagic.net/librarys/veda/detail/1634.