Live data from Hacker News

The Future of JavaScript

blog.chromium.org

1–10 of 115 posts

Re: The Future of JavaScript

#5

> No surprises, no more need to abuse objects as dictionaries. ... pretty much every single file of JS ever written is "guilty" of this.

I've only delved into really writing JS for the last couple of weeks, but a number of tutorials basically said that objects are dictionaries and vice versa, you just got some nice syntax and you can plug function in as values.

If that was an abuse, what are JavaScript objects supposed to be? What's the difference between them and a dictionary?

Re: The Future of JavaScript

#6
post #5

> No surprises, no more need to abuse objects as dictionaries. ... pretty much every single file of JS ever written is "guilty" of this.

I've only delved into really writing JS for the last couple of weeks, but a number of tutorials basically said that objects are dictionaries and vice versa, you just got some nice syntax and you can plug function in as values. If that was an abuse, what are JavaScript objects supposed to be? What's the difference between them and a dictionary?

It's only an abuse because JavaScript has no distinction between the notion of an object's "meta" properties, and its dictionary-like keys and values. So you'll get along just fine, until someone tries to write this:

    var dict = {};

    dict["hasOwnProperty"] = true;

    dict["toString"] = "Bob";

    dict["valueOf"] = 7;
... then you're in for a rough time.

Re: The Future of JavaScript

#7
post #5

Earlier quoted context omitted.

I've only delved into really writing JS for the last couple of weeks, but a number of tutorials basically said that objects are dictionaries and vice versa, you just got some nice syntax and you can plug function in as values. If that was an abuse, what are JavaScript objects supposed to be? What's the difference between them and a dictionary?

It's only an abuse because JavaScript has no distinction between the notion of an object's "meta" properties, and its dictionary-like keys and values. So you'll get along just fine, until someone tries to write this: var dict = {}; dict["hasOwnProperty"] = true; dict["toString"] = "Bob"; dict["valueOf"] = 7; ... then you're in for a rough time.

Another problem with objects-as-dictionaries is that the keys must be strings. If you use a different type as a key, it is implicitly converted to a string, which can lead to some surprising problems for programmers who don't expect it.

Maps/Set/WeakMap properly support arbitrary objects as keys.

Re: The Future of JavaScript

#8
Anyone know where can I request a OrderedMap? That committee Wiki seems hardened against intruders.

Would be really unfortunate to have to wait for a native OrderedMap/SortedMap/TreeMap until ES7.

Re: The Future of JavaScript

#9
post #5

Earlier quoted context omitted.

I've only delved into really writing JS for the last couple of weeks, but a number of tutorials basically said that objects are dictionaries and vice versa, you just got some nice syntax and you can plug function in as values. If that was an abuse, what are JavaScript objects supposed to be? What's the difference between them and a dictionary?

It's only an abuse because JavaScript has no distinction between the notion of an object's "meta" properties, and its dictionary-like keys and values. So you'll get along just fine, until someone tries to write this: var dict = {}; dict["hasOwnProperty"] = true; dict["toString"] = "Bob"; dict["valueOf"] = 7; ... then you're in for a rough time.

var map = Object.create(null);

..will not have any of those properties. Works only in v8 though, AFAIK.

Post reply on HN