Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It looks to me like what happens is exactly what you'd expect, i.e., you end up with a function defined under the name "func", just as you would with any other function name. Is there something I'm missing?


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.


There 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.)




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: