Hacker News new | past | comments | ask | show | jobs | submit login

If one is trying to find redundancy in the language, one might want to ignore the redundant items in the list first:

* 2 and 8 are literally just 1 inside of a callback, which is weird to be this list. Might as well also have `var x = console.log(1);` while we're at it.

* 11 is some Nodejs specific way of making a new context and then running code on it. It's somewhat equivalent to doing this in bash: `echo "#!/bin/bash\necho 123" > script.sh && bash script.sh`. I think it's pushing it to call that a way to run a function within the language.

A few are natural consequences of how the `this` parameter and classes work in Javascript. (Not to say that the way the `this` parameter and classes work in Javascript is above criticism, but they do make certain kinds of behavior easy that would require relatively painful reflection in Java to do, so if anything one should compare them to Java's reflection APIs.)

* 4 shows the call method of functions, which lets you provide a different `this` parameter to a function call than the automatically inferred one. (In a regular call to `console.log(1)`, the log function from the console object is called with console as the `this` parameter and 1 as the first normal parameter.)

* 9 and 10 are just demonstrations of that all methods can be retrieved from the object that owns them or the class they came from.

Some of the kinda redundant-looking ones have uses which I think pays for their existence:

* 6 is using an eval-like shortcut to parse a string into a callable function rather than executing it immediately, and unlike eval, it doesn't get access to variables within the current scope, which makes the code easier to analyze. Anywhere `eval` is used with a dynamic string, you've got to wonder about how it could screw with variables within the current scope.

* 5 is the way to supply a function with a variable number of arguments supplied by an array. ES6 has added some nice syntax sugar for this at least: `console.log(...someArray)`.

* 7 shows the `Reflect.apply` function, which is equivalent to `Function.prototype.apply.call`. The global `Reflect` object is provided as a convenience/demonstration as a default no-op pass-through implementation of a Proxy handler. There's almost no need to ever use the Reflect object outside of setting up a Proxy object. Calling it redundant would be like drawing an equivalence between tissues and toilet paper and wondering why anyone bothers buying toilet paper separately: it's because it's put into a convenient form-factor for a specific use.




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

Search: