3 ms·
Working for a web company. Our favourite JS question is, "What happens if you do this?" var func = function func () { // blah };
by solox3 13y ago
Working for a web company. Our favourite JS question is,
"What happens if you do this?"
var func = function func () {
// blah
};
- jbrooksuk 13y agoThe function is able to reference itself, allowing recursion. This used to cause an error in Safari though, am I right?
- aaronem 13y agoThat doesn't seem special: var foo = function (n) { return foo(n); }; foo(1); gives back "too much recursion", not "foo is not defined", in both Spidermonkey (Firefox 24) and V8 (node.js 0.10.something).
- Aaronneyer 13y agoA function can reference itself without defining the name twice. These both work just fine: var factorial1 = function(n) { return n < 1 ? 1 : n * factorial1(n-1) } function factorial2(n) { return n < 1 ? 1 : n * factorial2(n-1) }
- jrawlings 13y agoRecursive functions like this are more like closures and not pure functions. Aliasing factorial1 and reassigning it will break the recursion [1]. Maybe this is what jbrooksuk was referring to. [1] https://leanpub.com/javascript-allonge/read#recursive https://leanpub.com/javascript-allonge/read#recursive
- hacknat 13y agoThis is a bad FizzBuzz question, in my opinion.
- solox3 13y agoIt's one of those questions that provoke the most thought, allow granular benchmarking of the interviewee's knowledge, and have a valid answer, in my opinion. That is to say, this question has worked for us as a dev house, maybe not so much if you're hiring contractors, I can't say for sure.
- hacknat 13y agoThat sounds more like a great interview question than a good FizzBuzz question though. FizzBuzz questions are all about assessing, in less than 30 seconds, if you should even be giving the person on the phone the time of day.
- aaronem 13y agoIt 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?
- xutopia 13y agoThere 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.)
- sequoia 13y agonothing? What answer are you looking for here? either a) there's something I'm totally missing or b) this is a really silly "trick" question.
- 3minus1 13y agoIt's a good chance to show off your knowledge of hoisting.
- sequoia 13y agohm. Maybe it's just because I don't personally excel at this sort of question, but I don't like "trick" questions like this where the interviewer is not forthright with what they're looking for- another way to describe this is "playing mind games" with someone, which is typically considered impolite/antisocial. I much prefer "Here's a domain problem, how would you solve it" to a vague question where the interviewer has a specific, hard to determine answer in mind. The former directly tests my ability to solve relevant problems, the latter just tests my ability to guess what the interviewer is thinking. In this case I'm perfectly aware of how variable & function declarations are hoisted in JS, but it wasn't clear to me that this was the answer sought- I know the answer but I don't know that's the answer being angled for so I fail at the question. Should this reflect badly on me? I am not sure- this sort of guessing skill may be what this interviewer is looking for. (?)
- deleted 13y ago[deleted]
- deleted 13y ago[deleted]
- ShellfishMeme 13y agoThe variable declaration is hoisted to the top, then is assigned a named function expression `func` so that `func.name === 'func'`. Useful for proper error messages and stack traces. Something else I'm missing?
- thomaslangston 13y agoCould you go into more detail about why you would do A) var func = function func() { /* blah */ }; Instead of B) function func() { /* blah */ };
- yuchi 13y agovar func = _.memoize(function private_non_memoized_func() { func(); // "light" private_non_memoized_func(); // "heavy" });
- ShellfishMeme 13y agoLet's say you want to define a function to be used for a certain operation on two numbers dependent on some boolean. if (addition) { // operation = add } else { // operation = subtract } If you use function declarations you might run into bugs if (addition) { function operation (a, b) { return a + b; } } else { function operation (a, b) { return a - b; } } console.log(operation(1,2)); Here it might happen that you don't get the expected function. Additionally it's much less clear what's going on. Compare it to this: var operation; if (addition) { operation = function add (a, b) { return a + b; }; } else { operation = function subtract (a, b) { return a - b; }; } console.log(operation(1, 2)); Here it's very obvious what's going on and your operation function has a name that reflects what it does. Personally I prever never to rely on function hoisting and always declare functions using function expressions. It keeps the code style more consistent and it's clearer what is happening in your program.
- deleted 13y ago[deleted]
- solox3 13y ago
- yuchi 13y agoI teach JS. I use a a stronger one: var func = function func(func) { var func; console.log(typeof func); }; function func(func) { console.log("func"); } func("func");
- deleted 13y ago[deleted]