Live data from Hacker News

New Features in ES2019

javascript.christmas

71–80 of 123 posts

Re: New Features in ES2019

#71
post #48

Earlier quoted context omitted.

Weak types. When neither the function nor the parameters have hard types, you have to create heuristics. There could be a test for numbers there, but it would also be surprising because at the older days people expected "10" and 10 to behave the same.

Most would expect 10 to parse as an integer. To specify a string, most would be happy with putting quotes around it. So I don't think dynamic types fully explains the bizarre behaviour.

What if you tried to sort an array of objects or functions?

Re: New Features in ES2019

#72

Not ES directly, but you know what I need on an almost daily basis? A JSON date type. It’s obnoxious to have to pass a string back and forth and parse it on either end between server and browser.

There's plenty of good reasons one doesn't exist. JSON is not JS-specific. It is a standard interchange format used by thousands of languages, many of which have very different date implementations. If you did have a JSON date, how would you decide what it was? Would it be a timestamp, or a civil date-time? Would it have timezones? Offsets? Locations? Would there be a database along with it required to understand it…

That is not a good reason though. The spec could just decide on the wire format like with strings and numbers, and clients would have to translate into the date type of the language or platform.

The reason JSON doesn't have dates is simply because JavaScript doesn't have date literals. You can write new Date(...) in JavaScript, but allowing that in JSON would have opened a whole can of worms.

Re: New Features in ES2019

#73

Not ES directly, but you know what I need on an almost daily basis? A JSON date type. It’s obnoxious to have to pass a string back and forth and parse it on either end between server and browser.

Clojure devs tried to make something like that with edn and transit IIUC

Does anyone use edn or transit outside of the clojure ecosystem?

Re: New Features in ES2019

#74
post #61

Earlier quoted context omitted.

I know how it works :) Since it's part of the API getting a response object is pretty effortless, which is a better word than automatic, agree.

That's right, but parsing JSON into a JavaScript object will not give you a date object, which is what OP was referring to.

My bad, probably misread date to data :)

Re: New Features in ES2019

#75

> Optional catch binding Eh... that's great and all, but why not go the whole way and allow me to just use `try { }` without a catch-block? I'm sure that was part of a conversation somewhere, and I wonder why they chose not to go that far.

Or not require {} everywhere even for single statements, a la F#:

  try foo() with | ex1 -> failWith "doh!" 
and allowing these to be expressions rather than just statements. Exceptions become much easier to deal with this way:

    let result = try compute() with ex1 -> failwith "doh!" | ex2 -> -1

Re: New Features in ES2019

#76
post #32

Earlier quoted context omitted.

> One thing that I am a bit afraid about though is that the language might become more complex and complicated over time because of backward compatibility (C++-like?). New methods with simple behaviour don't really make the language more complex through. > And that's great, but that's a new function, and maybe 'replace(...)' should have had this behavior from the start. As you note it kinda does, just in a weird roun…

> New methods with simple behaviour don't really make the language more complex through. I think they do, in some ways. If you have multiple methods or functions which seem to do similar things, as a new-comer it can be incredibly confusing. I remember when I started I never knew what the "best way" to iterate on things was... for loops? for in? for of? foreach? And it was not obvious which one to use or what were th…

The biggest complexity comes with interactions between features.

For example, "move semantics" in C++ -- while useful -- trigger tons of questions about interactions with other features.

Duplicate features (for-of/forEach) are a problem, but a lesser one.

---

FWIW,

* Array.prototype.forEach iterates over an array

* for-of iterates over an iterable, including arrays

* for-in iterates over object keys (strings)

To your point, for-of is the more general form of forEach, so IMO there isn't much reason to use it now.

Re: New Features in ES2019

#77
post #32

Earlier quoted context omitted.

> One thing that I am a bit afraid about though is that the language might become more complex and complicated over time because of backward compatibility (C++-like?). New methods with simple behaviour don't really make the language more complex through. > And that's great, but that's a new function, and maybe 'replace(...)' should have had this behavior from the start. As you note it kinda does, just in a weird roun…

> New methods with simple behaviour don't really make the language more complex through. I think they do, in some ways. If you have multiple methods or functions which seem to do similar things, as a new-comer it can be incredibly confusing. I remember when I started I never knew what the "best way" to iterate on things was... for loops? for in? for of? foreach? And it was not obvious which one to use or what were th…

Learning about many simple features has a cost, but I much prefer that to inherently complex things that I'll always be confused by.

Ruby excels at this. There are 5 ways to do everything, but they're all simple and readable.

Re: New Features in ES2019

#78

> Optional catch binding Eh... that's great and all, but why not go the whole way and allow me to just use `try { }` without a catch-block? I'm sure that was part of a conversation somewhere, and I wonder why they chose not to go that far.

I expect it's because of `finally`. You can write a try without a catch today, provided there's a finally, however the error will continue to propagate in that case.

    try {throw new Error('')} // exception is caught
    
    try {throw new Error('')} // uncaught exception
    finally {console.log('finally')}
That would be a bit of a footgun, as adding a finally clause that does something innocuous like logging would result in an uncaught exception!

Re: New Features in ES2019

#79
post #78

> Optional catch binding Eh... that's great and all, but why not go the whole way and allow me to just use `try { }` without a catch-block? I'm sure that was part of a conversation somewhere, and I wonder why they chose not to go that far.

I expect it's because of `finally`. You can write a try without a catch today, provided there's a finally, however the error will continue to propagate in that case. try {throw new Error('')} // exception is caught try {throw new Error('')} // uncaught exception finally {console.log('finally')} That would be a bit of a footgun, as adding a finally clause that does something innocuous like logging would result in an u…

That makes sense. I guess `try` means something a bit different in JavaScript than `rescue` does in Ruby.

It seems like this code in Ruby:

```

begin

  do_something
rescue => e

  handle_error e
ensure

  log_something
end

```

is equivalent to this in JavaScript:

```

try {

  doSomething();
} catch(e) {

  handleError(e);
} finally {

  logSomething();
}

```

I'm not saying that they're 100% equivalent, but I was thinking of the `try` block as if it's `rescue` when really it has more in common with `begin` in Ruby in that it's defining a block of code that can be rescued/caught if an error occurs.

So I guess my confusion comes from this type of block being named `try` rather than something like `do` or `begin`.

TL;DR I was just thinking about this in the wrong way from what I can tell.

EDIT: Yet I think that my point might still stand in that, if `try` is just a block that has its own scope, like an if-block or `begin` in Ruby, then it should be possible to not require either `catch` or `finally` since its own behavior has little to do with error handling.

For instance, this is possible:

```

let x = 'foo';

try {

  let x = 'bar'; 
} catch(err) {

  // noop
}

console.log(x); // foo

```

The try-block would be way more useful if it could just define scope without being tied specifically to error handling. There are lots of circumstances where defining scope would be handy outside of conditionals and creating functions for a similar purpose.

I guess what might have made the most sense, if JS could have started over, is that there'd be no `try` statement and that `do` could be used in its place.

```

const words = ['foo', 'bar', 'baz'];

let i = 0;

do {

  console.log(words[i]);
  
  i++;
} catch(err) {

  console.error('whoops!');
} while (i ```

We can't go back now since JS, for good reasons, is remaining mostly backwards-compatible. But the ability to do the above without extra gymnastics would be pretty cool.

Re: New Features in ES2019

#80
post #48

Earlier quoted context omitted.

Most would expect 10 to parse as an integer. To specify a string, most would be happy with putting quotes around it. So I don't think dynamic types fully explains the bizarre behaviour.

What if you tried to sort an array of objects or functions?

If one doesn't specify a comparator and there is no obvious/sensible default, then an error would be perfectly reasonable.
Post reply on HN