background preloader

Named function expressions demystified

Named function expressions demystified
Introduction Surprisingly, a topic of named function expressions doesn’t seem to be covered well enough on the web. This is probably why there are so many misconceptions floating around. In this article, I’ll try to summarize both — theoretical and practical aspects of these wonderful Javascript constructs; the good, bad and ugly parts of them. In a nutshell, named function expressions are useful for one thing only — descriptive function names in debuggers and profilers. I’ll start with a general explanation of what function expressions are how modern debuggers handle them. Function expressions vs. One of the two most common ways to create a function object in ECMAScript is by means of either Function Expression or Function Declaration. We can see that when identifier is omitted, that “something” can only be an expression. A somewhat less obvious example of function expression is the one where function is wrapped with parenthesis — (function foo(){}). To demonstrate with examples: Tests

Getting Out of Binding Situations in JavaScript Most developers don’t know about—or don’t care enough about—binding in JavaScript. Yet this single issue is responsible for a sizeable portion of the questions on most JavaScript-related support channels, and thousands—if not millions—of hairs being tortured away from developer heads every single day. Yet with a little attention to this oft-overlooked subject, you can avoid wasting your time, energy, and patience and move on to more powerful, efficient scripting. Why should we care about binding? Almost no major object-oriented programming (OOP) languages force you to consider binding. PHP and JavaScript do require you to explicitly state which object you’re accessing, even if it’s the current one. Of course, neither PHP nor JavaScript are actually object-oriented in the traditional sense. In JavaScript, binding is always explicit, and can easily be lost, so a method using this will not refer to the proper object in all situations, unless you force it to. Let’s step in it#section2 Ah-ha!

JavaScript: la référence - David Flanagan