5 ms·
As a developer, I've always liked languages that stress maps/hashes/dictionaries/tables/associative-arrays as their man data structure. Off topic, but I've alw
by russellbeattie 8y ago
As a developer, I've always liked languages that stress maps/hashes/dictionaries/tables/associative-arrays as their man data structure.
Off topic, but I've always thought that JavaScript and JSON would have been a lot better without the relatively artificial distinction between objects and arrays. Now that there's object shorthand and destructuring, this will never happen, but it was a nice thought for a while.
- chmod775 8y agoNobody is stopping you from just using arrays for everything - remember they're also objects. const a = [2,3,…] a.prop = "hi" Now whether that's actually something you should be doing is a different matter...
- russellbeattie 8y agoI was thinking more along the lines of using "objects" for everything like Lua: let foo = {'a', 'b', 'c'}; . (Also on my JS wish list: Killing the use of const for anything besides actual constants.)
- anonytrary 8y ago> Killing the use of const for anything besides actual constants You should be using const for everything besides variables that need to get re-assigned.
- enriquto 8y agoso, in a saner language, variables would be const by default
- russellbeattie 8y agoI just recently learned Rust's 'let' works that way, unless you add the 'mut' keyword as well. (And const, of course, is always immutable.)
- russellbeattie 8y agoHeh, I like how you carefully worded that, because normally 'const' would signify that a variable is immutable in just about any other programming language in the world. Except in JS, where const was awkwardly jammed into the language. I guarantee if the spec writers had known how badly const was going to be abused, they wouldn't have added it. If you want a strongly typed language, go use C or Java and stop making JavaScript incomprehensible to all but the most pedantic developers.
- zbentley 8y ago> normally 'const' would signify that a variable is immutable in just about any other programming language in the world That's not true in Java, C, Ruby, or Perl (with the 'constant' prag-module), not really true in C++ (unless you're careful), and . . . generally just isn't true in most languages. While a few languages do add recursive immutability properties to their equivalent of 'const', most languages use the idea of a 'constant' only as a means of prohibiting name re-binding, not data modification. And even then, many languages that only prohibit const re-binding only do so in an optional (i.e. it's a warning) way. Many of those same languages do support immutable data structures either out of the box or via popular libraries (though in non-compiled language, such structures often come with a performance cost). They just don't call them const[ant]s. Edit: I'm also curious about how you think 'const' is being abused in JS. I'm not an expert in the language--this is a genuine question; not a veiled disagreement.
- anonytrary 8y agoAs long as you understand how `const` works in JS, I don't see it as in impediment. const x = {}; x.foo = "bar"; is not that confusing, because you aren't re-assigning x, you're mutating its data. Maybe `fixed` would have been a better name for it (like a vector that doesn't change direction but can change magnitude) if people think `const` should mean immutable.
- hajile 8y agoThat's not quite true. Const forces a variable to always point to the same object, but doesn't make the object immutable. Only primitives like bool, string, or number are actually guaranteed to be constant. The following is perfectly valid code, but breaks almost everything someone would reasonably expect from const. const foo = {bar: 123}; delete foo.bar; foo.baz = 456;
- wtetzner 8y agoThis seems to be in agreement with GP: > You should be using const for everything besides variables that need to get re-assigned.
- hajile 8y agoYou wouldn't start coding python for the first time without some research. You wouldn't for Java, ruby, C, bash, or any other language you can name. JS is the one language that very few people bother to learn. They just start writing and see what works. For these people, a const that isn't const isn't a very good idea. Even among the good JS devs I know, most haven't learned this until it actually bit them in production because most books about JS don't mention the problem. The TDZ (temporal dead zone) is a direct result of adding `const` to the language and it complicates everything (plus has performance implications). A feature that doesn't do what it seems to do while complicating everything else about the language is a textbook example of bad design. There are several people who agree in hindsight (I believe Brendan Eich is among them).
- anonytrary 8y ago`const` "seems to do" this: prevent variables from getting re-assigned. That's all one needs to know to understand the associated pitfalls.
- BrendanEich 8y agoFrom C99 and C++, also Java, `const` does not affect the bound value. It's a declaration keyword, so it affects only how to bind the declarator name or names that follows immediately. This is all stating the obvious, but expecting effects on the initialiser will lead only to frustration, on two counts: 1. JS is in the C/C++/Java lineage syntactically. Ok, that is a deeper design flaw, but Netscape management to keep Sun on board said "Make It Look Like Java". 2. If you want declarations to affect captured expressions, you'll soon want a type system if not immutability controls and defaults. IOW, at least Scheme-like binding rules and set! as special form to use when mutating. Again the ships sailed 23+ years ago, and in truth for "Mocha's" target audience, immutable-by-default would have been harder to use. "They just start writing and see what works" => a primary design goal. Flawless Victory! :-) Seriously, the Scheme in the browser baby was killed in the cradle; the recruiting bait used to get me on board was snatched away because of the Sun/Netscape Java deal. All is not lost, as JS grows better special forms (but for backward compatibility must keep old ones). Among these, `const` is least better but still useful. What's more, those who advocate mass-market immutable-by-default are still working to make it usable in the large (e.g., in Rust). Pedagogy helps, but also has not delivered the goods yet. We are living through the full experiment to refine and confirm that i-b-d is best.
- hamandcheese 8y agoThe distinction is syntactically important for JSON, IMO. Nobody wants to write integer keys for each element in an array.
- trynumber9 8y agoYou don't have to { 1, 2, 3, Prop = 'Example' } is the same as { [1]=1, [2]=2, [3]=3, ['Prop'] = 'Example' }.
- trynumber9 8y agoExample: https://repl.it/repls/ConcreteIntelligentBootstrapping https://repl.it/repls/ConcreteIntelligentBootstrapping
- kybernetikos 8y agoThe javascript spec makes it clear that an 'array' is just an object with a magic length property that is always one bigger than the largest set numeric index. The array syntax can be thought of as a shorthand where you want the indexes inferred from order instead of having to list them out. There are obviously a few useful methods that you use on arrays but almost all of these will work on any object with a length if you .call or .apply them.
- nly 8y agoSpeaking of JavaScript. For a lightweight, Lua-like, embedded JS runtime there's always Duktape https://duktape.org/ https://duktape.org/
- kibibu 8y agoYou can always use PHP! A PHP array works as a sequential or an associative array; or both at the same time
- heavenlyblue 8y agoYou can also use a donkey as a car.
- deleted 8y ago[deleted]