If a function has a name, the debugger displays its name on the called Stack during debugging. Some debuggers
If a function has a name, the debugger displays its name on the called Stack during debugging. Some debuggers (Firebug) Sometimes name your functions and display them to share the same role with the convenience of applying them, but normally, these debuggers are named by simple installation rules, so there is little value. Let's look at an example:
Function foo () {return bar ();} function bar () {return baz ();} function baz () {debugger;} foo (); // here we use three function declarations with names. // when the debugger goes to the debugger statement, firebug's call stack looks very clear // because it clearly displays the name baz bar foo expr_test.html ()
By viewing the call stack information, we can clearly understand that foo calls the bar, barand calls baz(and fooben is called in the global scope of the expr_test.html document). However, there is a better place, this is the function that Firebug named anonymous expressions:
Function foo () {return bar ();} var bar = function () {return baz ();} function baz () {debugger;} foo (); // Call stack baz bar () // have you seen it? Foo expr_test.html ()
Then, when the function expression is a little more complex, the debugger is not so intelligent. We can only see the question mark in the call Stack:
Function foo () {return bar ();} var bar = (function () {if (window. addEventListener) {return function () {return baz () ;}}else if (window. attachEvent) {return function () {return baz () ;}}) (); function baz () {debugger;} foo (); // Call stack baz (?) () // Here is the question mark: foo expr_test.html ()
In addition, when a function is assigned to multiple variables, the following depressing problems also occur:
function foo(){ return baz(); } var bar = function(){ debugger; }; var baz = bar; bar = function() { alert('spoofed'); }; foo(); // Call stack: bar() foo expr_test.html()
At this time, the call stack shows that foo calls bar, but this is not the case. The reason for this problem is that baz and another one contains alert ('spoofed ') the function is caused by reference swap.
In the final analysis, only getting a name for a function expression is the most delegate method, that is, using a namefunction expression. We will use an expression with a name to rewrite the above example (note that the names of the two functions returned in the expression block that is called immediately are bar ):
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 (); // The call stack information is displayed again! Baz bar foo expr_test.html ()
OK. I learned another trick, right?
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/1635.