2 ms·
There are two ways to define a function: var func = function(){}; function func(){}; The example above is doing both at the same time... don't know if
by xutopia 13y ago
There are two ways to define a function:
var func = function(){};
function func(){};
The example above is doing both at the same time... don't know if it's a mistake on their part or not.
However they're probably wondering if the user knows about scope and other such things. Inside the brackets we have a new scope but it has access to top level scope as well. They want to know that you know that stuff.
Try any of the two lines above in a js console (in browser) then type:
window.func
this.func
func
The result should be the same.
- aaronem 13y agoThere are three ways to define a function in Javascript; the third is exemplified in the example above, although it's not usually done with the same name for the variable and for the function. The value of naming a function as you assign it to a variable is that, when an exception arises within the function body, your error log output contains the function name, which doesn't always happen when you simply do "var foo = function () {...}". (It's also handy when you're using an editor which doesn't understand Javascript well enough to understand that "var foo = function () {...}" actually creates a function named "foo"; in such cases, explicitly naming the function clues in the editor's parser, which simplifies code navigation.)