4 ms·
It's only WTF because it's not the same as other implementations of map. Once you can internalize the map implementation, it's no longer WTF & actually makes se
by briantakita 13y ago
It's only WTF because it's not the same as other implementations of map. Once you can internalize the map implementation, it's no longer WTF & actually makes sense.
- rplnt 13y agoThat's the weird part. Why does array provide three arguments? But I agree, that's something you can learn. I guess. But it's WTF anyway. I have a function that takes either one or two arguments, I provide three, and everyone seems to be OK with that.
- just2n 13y agoWell that decision is pretty necessary when you realize that JS has no syntax to indicate a function is variadic (we use the arguments magic variable, but use of it does not necessarily indicate that a function is variadic) and that implementation supplied functions are not required to have their arity exposed via Function.prototype.length (http://es5.github.io/#x15.3.5.1 http://es5.github.io/#x15.3.5.1). There's no way to know, even at run-time, whether a function is being called with too few or too many arguments, since that's equivalent to the halting problem. So the sensible alternative is just to default everything to undefined, and silently ignore extraneous arguments. But yes, if JS was strict with how it handled argument definition lists and had support for indicating infinite arity, I'd agree, this would be a WTF, or at least strange. But I think it makes a lot of sense, all things considered.
- meowface 13y agoIt makes sense but it still strongly violates the principle of least surprise. No other language I know of does this, nor do I think this would be a particularly desirable feature.
- gnuvince 13y agoSo much for abstraction if you need to understand the implementation of every function you'll every use in JavaScript.
- yourad_io 13y ago> understand the implementation of every function Rather: remember three things that make up the majority of Array iterators' callback functions' signatures: Element, Index, Array. Shared by: .map, .every, .forEach, .filter, and probably some that I am forgetting. The exception I think is just .reduce[Right], which by definition requires its previous return value, so you have (retVal, elem, i, arr). Quite literally, if you remember .map callback, you remember .every callback :) Javascript deserves shtick for its truly bad parts (with, arguments, ...) and some missing parts, but .map and its friends aren't it.