So what are your thoughts on the latest going-ons in the world of JavaScript? I’d be particularly interested to hear your thoughts on testing and the debate kicked off by DHH with his “TDD is dead” post. I mean, we’re all testing our code, right?
I couldn’t find in var functionName = function() {} vs function functionName() {} where it covered the main reason for using ar functionName = function() {} (which I’d have expected to see near the top so I didn’t read all the way to the end).
Using var functionName = function() {} allows you to redefine the function - this is particularly useful when done from within the function itself.
There are a number of situations where you need to use feature sensing to detect which of two or more commands that the browser currently in use supports for providing particular functionality. Typically these will get run multiple times by a given visitor. Wrapping these tests inside a function and having the function rewrite itself means that you only need to test which variant the browser supports once. The function then rewrites itself to always use that variant for the subsequent calls without rerunning the test.
For example the following defines an addEvent function that will work in both JavaScript and JScript. The first time the function is called the function works out which of addEventListener (JavaScript) and attachEvent (JScript) that the browser supports and it rewrites the addEvent function so that it will always use the one that the browser supports without needing to restest which is supported. The code defines the addEvent function three times with different code for each. This is something that you can’t do using function functionName() {} and so if you want to use just one way consistently then you need to use var functionName = function() {} so that you can handle situations like this one.
So you now have one function functionName() {} but the other two are still functionName = function() {}
So where my code used one of the two ways consistently, yours uses one way for one call and the other way for the other two calls and so is jumbling the two ways together in the same code.
As I said - if you want to use one of the two approaches for all calls then var functionName = function() {} is the only one that can be used for all function definitions.
There are THREE definitions of the addEvent function in that code - not one.
Ahh. I suppose I misunderstood your post. I thought you were saying that a function declaration couldn’t be overwritten. But you were actually saying that you need a function expression to do the overwriting.
That approach is still effectively using both ways of defining a function since addEvent = addEventListener; is still defining a function using an assignment.
Also that approach uses five statements that define functions instead of three.
I agree that there is no reason that you can’t use both ways in your code - as both of your examples show - my point was that if you want to use just one way consistently in your code then only one of the two allows you to do it.