12

Today I came across a question How to sum the values of a JavaScript object?. I noticed that using latest ECMAScript features the problem can be solved with shorter, simpler and (IMHO) easier‑to‑understand code, so I decided to post an answer.

I expected that people will appreciate my effort to provide a more up‑to‑date answer, but instead I got 2 downvotes (that was in the moment of writing this question, now it has score of 0). Someone also posted a comment complaining that:

  1. It's not worthy to include a 60K library (if we're talking about babel-polyfill, it's actually only 32.6K gzipped) just to have Object.values().
  2. It's 4 times slower than solution using for loop.

I don't quite agree with these arguments.

  1. You have to use Babel anyway if you want to use any new ECMAScript features and still support older browsers. And I'm not talking only about ES 2016/2017. For example IE 11, which still is still used by many people, supports only 11% of ES 6 (according to kangax ES compatibility table).
  2. I think that "4 times slower" isn't that much unless you're working with a huge amount of data (which doesn't happen to most people). Also, even if it's 4 times slower now, it doesn't mean that JavaScript engines developers won't improve performance of the reduce() method in the future.

Don't get me wrong, I'm not trying to prove that someone is not right. I just want to be helpful to the community. If answers like the one I posted aren't welcome, I can understand that.

6
  • 5
    Probably because it's experimental (still in draft), not because it is novelty.
    – Braiam
    Commented Aug 20, 2016 at 0:07
  • @Braiam Yes, but even that it's unlikely that it's going to change that much that the code in my answer would stop working. Commented Aug 20, 2016 at 0:09
  • I'm just trying offer a reasoning behind the downvote...
    – Braiam
    Commented Aug 20, 2016 at 0:17
  • 2
    Not quite sure why you're taking this comment so seriously. Move on. People who make such comments are some combination of dinosaurs stuck in the past and/or ignorant about the current state of the world.
    – user663031
    Commented Aug 20, 2016 at 10:05
  • 3
    I find "full disclosure" will sometimes fend off downvotes. With mobile apps, it's often a big hit to include an extra 30+K library to gain 1 bit of functionality that can be gained natively without the 30K hit. So ... disclose that this is an ES6 capability (you did, sort of -- specify ES6) and that the Babel library can provide ES6 capabilities if the design permits an additional 30K download cost. :-)
    – markE
    Commented Aug 20, 2016 at 18:15
  • If you trust kangax table more than MDN's the Object.values() is available on Edge 14 and Chrome behind a flag
    – Braiam
    Commented Aug 20, 2016 at 18:59

3 Answers 3

12

Are answers using latest, standardised ECMAScript features discouraged?

Not at all. If the question is tagged , feel free to answer with anything that is in the latest standard. However, if the question is not tagged or , or already showing current features in the question code, you should add a statement being explicit about the version you are using.

Are answers using experimental, not widely supported ECMAScript features discouraged?

Possibly. Unless the question does ask for them or there's really no other good solution yet, you should limit yourself to features in a higher stage of the standardisation process.

In any case, you will at least have to explicitly link the proposal that drafts the new feature, and state that is experimental only. The phrase "Using latest ECMAScript features" is certainly not enough, not in a language that is changing as quickly as JavaScript. Be specific.

2
  • So ES 2017 counts as experimental? Commented Aug 20, 2016 at 20:28
  • 1
    Sure. The latest approved standard as of now is ES2016 (ES7). Once a feature has been integrated into a released standard, you can of course edit your answer. But we really don't want the situation again of people being mislead about what will get into ES7 (as it turned out, almost nothing).
    – Bergi
    Commented Aug 20, 2016 at 20:57
6

Are answers using latest, not widely supported ECMAScript features discouraged?

Certainly not. I've often seen ES6 answers posted to new questions rise above ES5 or ES3 answers. This question did not use any specific ES version tags, so I would say any version is fair game (except maybe the abandoned ES4).

I expected that people will appreciate my effort to provide a more up‑to‑date answer, but instead I got 2 downvotes.

Unusual, but people vote as they see fit. Maybe they voted because of the file size issue, maybe because the for loop is faster [citation needed], maybe someone thought that wasn't valid JavaScript syntax (been known to happen).

0
-2

If the question is visibly using ES6, sure, answer with ES6. If the question is using ES5, answer with ES5.

In other words, use what version the OP is using, if you care about your answer getting accepted.
Of course there's no harm in adding an ES6 example as well.

If the OP's declaring functions the "Old fashioned" way: function(){}, he might not be familiar with the ES6 syntax. When in doubt, at least make sure you mention the syntax being ES6, with some added explanation.

16
  • 10
    But I'm answering not only for the OP, but also for other people visiting this question. And anyway "he might not be familiar with the ES6 syntax" is not a reason for not using it. People should have the opportunity to learn something new from answers on SO. Commented Aug 20, 2016 at 11:30
  • Sure, but that "something new" should be within the scope of the question. You don't answer angular1 questions with angular2 answers. If the OP is visibly (not) using a specifiv version, (don't) use that version.
    – Cerbrus
    Commented Aug 20, 2016 at 11:31
  • And it is within the scope of the question. I used only these ES features that were required to solve the OP's problem. Commented Aug 20, 2016 at 11:33
  • 3
    Also, your answer implies that there should be separate questions "how to do something" for every version of JavaScript. Unfortunately JavaScript versions doesn't work like versions in other languages, or even like in JavaScript libraries/frameworks like Angular. Nobody is using a specific version of JavaScript, because different browsers support different subsets of JavaScript. Commented Aug 20, 2016 at 11:36
  • All I am saying is: Answer in the version the OP is using. If you want to, also add an explanation of how to do it in the latest version.
    – Cerbrus
    Commented Aug 20, 2016 at 11:44
  • Let's put it this way: Q: "I want to do X, it has to be compatible with older browsers", then A: "Do <Y>, but you need to use a transpiler" is a horrible answer.
    – Cerbrus
    Commented Aug 20, 2016 at 11:46
  • 4
    So then there's someone who also wants to do X, but he doesn't care about older browsers. Should that person ask a new question? (Also, "older browsers" is ambiguous.) Commented Aug 20, 2016 at 11:49
  • @Gothdo: Like I said, if you want to, also add an ES6 example in the answer. For someone that isn't using ES6, an ES6 answer doesn't answer the question. That's why you have to take a look at the OP's code. If you're not sure, add both.
    – Cerbrus
    Commented Aug 20, 2016 at 11:52
  • 4
    Who cares about what OP is using other that the web api and JavaScript? An answer can be added if offers another method to achieve the same result, independently of the constraints it was first asked.
    – Braiam
    Commented Aug 20, 2016 at 13:01
  • 1
    @Braiam: If the answer isn't compatible with the OP's system / requirements, it doesn't answer the question. However, in this specific example, I can see the value of adding an ES6 answer, as alternative to the existing ES5 one(s). Especially on a canonical question like that.
    – Cerbrus
    Commented Aug 20, 2016 at 16:30
  • @Cerbrus if a question includes code that doesn't happen to include ES6 features, it does not follow that they are definitely not using ES6, which is back-compat. Valid ES5 is valid ES6, so I think you have to look for signs that OP is looking to support older browsers or averse to transpilers, etc. to know you should limit it to ES5. Commented Nov 1, 2016 at 17:49
  • If the answer isn't compatible with the OP's system / requirements, it doesn't answer the question. Unless the question includes such information, I think it's good answer based on what works for the majority of browsers and let the OP treat those requirements separately from the specific question/answer. Compatibility issues are more general, cross-cutting concerns, and are unrelated noise in questions like the one being discussed. The same person can separately ask "how do I do ES6 thing in ES5?" or research transpilers/polyfills. Commented Nov 1, 2016 at 18:02
  • @jinglesthula: Is the OP is visibly using ES5 syntax where there is an ES6 alternative, you're not helping him by answering with an ES6 answer. If you disagree with my answer, that's fine, but I'm not going to discuss this again in comments, here.
    – Cerbrus
    Commented Nov 1, 2016 at 18:04
  • @Cerbrus no worries - definitely not looking to argue/rehash. Just offering some thoughts to consider as people weigh it over. Commented Nov 1, 2016 at 18:08
  • @jinglesthula: you're repeating what has been said before on a answer over 2 months old.
    – Cerbrus
    Commented Nov 1, 2016 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .