The trouble with new
We have a simple JavaScript class: [code lang='js'] function User(firstName, lastName) { this.nam
In JavaScript we have at least 4 ways of defining functions:
These are not all the same, and the crucial thing here is the word 'function' as used in each case. In the first example we're using the function statement, and in the second and third examples we're using the function operator. We'll come back to the fourth example later.
So what's the difference between the function statement and the function operator? Well first we need to understand a bit about anonymous functions. Most of us are familiar with using anonymous functions as event listeners - something like this:
In this example we've passed in a function without a name as a listener callback. But what do we mean when we say a function does have a name? Do we mean this:
No we don't. Assigning a function to a variable does not give it a name. The function assigned to our variable above is still an anonymous function. To give a function a name we need to do something like this:
Now we have declared a function with the name myFunctionName and assigned it to the variable myFunction. Giving a function a name in this way adds a read-only name property to it:
Coming back to our very first example, we can see that we're using a different form here - the function statement:
Under the hood, what this is actually doing is something like this:
The function statement created a named function and assigns it to a variable of the same name. Note that in this case although the function name and the variable name are the same, they don't have to be:
Let's take a look at the last of our four initial examples:
Functions defined this way are always anonymous, and cannot be given a name. In general you shouldn't define functions this way, for several reasons:
One last thing to note is that if you use the function operator, it has to be within the context of an expression. For example you can't do this:
That doesn't work because it's not part of an expression - the function isn't being assigned to anything and you get a syntax error. If you want to run an anonymous function and not assign it to a variable, it can be done like this, which runs the function straight away:
For further reading on this check out the Mozilla function reference docs.
To further explore JavaScript nuances, consider reading Answering Nicholas Zakas' JavaScript quiz, which delves into intricate aspects like type coercion and variable scope. Additionally, the post on The trouble with new offers insights into the correct use of the new
keyword, highlighting the importance of understanding this
context in JavaScript.
We have a simple JavaScript class: [code lang='js'] function User(firstName, lastName) { this.nam
A current meme that's floating around the JavaScript geek corner of the internet is setting quizzes
The Module Pattern is a way of using a closure in JavaScript to create private variables and functio
Writing a library is a balancing act between the (sometimes competing) interests of API clarity, cod