"What happens if you do this?"
var func = function func () {
// blah
};"What happens if you do this?"
var func = function func () {
// blah
};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.
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.
var func = function func(func) {
var func;
console.log(typeof func);
};
function func(func) {
console.log("func");
}
func("func");Something else I'm missing?
var func = function func() { /* blah */ };
Instead of B) function func() { /* blah */ }; 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.
var func = _.memoize(function private_non_memoized_func() {
func(); // "light"
private_non_memoized_func(); // "heavy"
}); 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.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.)
var factorial1 = function(n) { return n < 1 ? 1 : n * factorial1(n-1) }
function factorial2(n) { return n < 1 ? 1 : n * factorial2(n-1) } 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).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. (?)