One useful technique involves the practice of Debugging JavaScript by Redefining Functions. This method entails replacing the original function with a modified version that includes a debugger
statement, and then proceeding to execute the original function as intended.
Illustration
Instead of using an anonymous function, a named function is preferred for future reference.
MyApp.event.on("some:trigger", myFunc);
By storing a copy of the original function, it can still be called after debugging.
var oldFunc = myFunc;
myFunc = function() {
debugger; // alternatively, utilize console.log()
return oldFunc.apply(this, arguments);
}
This technique can be utilized in the Console or a code Snippet, ensuring that the code halts execution (or logs output with console.log
) upon reaching the designated section.
Advantages
The advantage lies in its ability to consistently halt execution at the trigger handler location, regardless of the number of times it is executed within the code. There is no need to manually step through each previous code segment using debugger controls. Simply stepping through once for a single case would suffice.