4 ms·
Try writing that same thing without ES6.
by ricardobeat 10y ago
Try writing that same thing without ES6.
- danyim 10y agoI'm also in the process of interviewing for a front-end engineer position, so thank you for replying and providing feedback. I'm assuming it has to be written this way because the function being pushed was referencing i, which was within the scope of the closure. Turning it into an IIFE breaks out of that. Thoughts? // Assuming pre-ES6 function iterate(k) { var funcs = []; for(var i = 0; i < k; i++) { (function(num) { funcs.push(function() { return num; }); })(i); } return funcs; } var itTest = iterate(4); console.log(itTest[0]()); // 0 console.log(itTest[1]()); // 1 console.log(itTest[2]()); // 2 console.log(itTest[3]()); // 3
- mathgeek 10y agoI'd probably ask you to do the printouts from within the function itself. If you haven't already read them, I highly recommend the You Don't Know JS series of books. One of them covers this exact question.
- weaksauce 10y agoneat series of books. thanks for the tip https://github.com/getify/You-Dont-Know-JS https://github.com/getify/You-Dont-Know-JS
- ehsanu1 10y agoIf you can figure that out during an interview, you're solid on the question. That's the gist of it, and knowing that you can use `let` to make it work is good too.
- ricardobeat 10y agoYup, that's what I was thinking of. As others have mentioned, if you can show this, and explain why `let` solves it too, you're good to go.
- pluma 10y agoTry writing the same thing without a computer why don't ya. As an interviewer I can see why you would want to force someone to jump through arbitrary hoops like that but if someone can show me `() => i` and explain why that works, that would be good enough for me.
- ricardobeat 10y agoNo need for the snark. There is still way more ES5 code being written than ES6. The point is demonstrating knowledge of closures and variable lifecycle, the how is irrelevant. I replied specifically because he said "I don't see the problem" - if you start with ES6 I can imagine it doesn't look like a problem at all.
- pluma 10y agoIf you can explain why that ES6 code doesn't share the problem, that's as good at demonstrating the knowledge you're looking for as restricting the candidate to an older subset of the language. I would accept "okay, now explain what distinguishes arrow functions from regular functions" as a follow-up question, but "now do it in ES5" seems arbitrary. In fact I would argue it's important that if the candidate shows practical knowledge of ES6 you need to make sure they also grasp the actual semantics (i.e. they won't rely on implementation details of Babel and run into errors in native ES6 environments). That said, if you are hiring for a dedicated frontend developer role, the majority of JS you write will have a build step. If it has a build step, you should use modern tooling. If you use modern tooling, you might as well include Babel to make the developers' lives less of a pain.