6 ms·
Huge bundle sizes and mutability are JS design flaws. Until very recently it was impossible to make objects immutable in JS. Even now its really only done usin
by throwaway189262 6y ago
Huge bundle sizes and mutability are JS design flaws.
Until very recently it was impossible to make objects immutable in JS. Even now its really only done using third party libraries.
Huge bundle size is only a problem because tree shaking is done on a "module" level. Most languages are static enough that it's called dead code elimination and doesn't need to use tree pruning. You can tell statically that the code is not used.
Moment isn't compatible with modern workarounds which I don't blame it for. The way JS is, immutability and true dead code elimination will probably show up as built in features in a few years and make all this obsolete again
- kqr 6y ago> Until very recently it was impossible to make objects immutable in JS. Even now its really only done using third party libraries. There's a big difference between "literally impossible to mutate" (only important in the most security-critical contexts) and "designed around immutability as the default" (which is what is desired in most of the real world.) The former not being possible is not a reason to exclude the latter.
- throwaway189262 6y agoWith JS, until recently, it was nearly impossible to design immutable API's. Everything takes JSON based objects which have no field accessors. All the objects underneath are just hashmaps with string keys. When all your objects are hashmaps you can't really make fields immutable. Even if you wrote getters, someone can just reassign the function reference. Even if you check your fields in incoming object parameters, somebody can randomly add more fields that you don't know about. Until recently you couldn't stop people from randomly adding fields to your objects Nowadays JS supports stuff like freeze thaw which makes it easier
- Ndymium 6y agoObject.freeze() has been available since IE 9, Firefox 4, Chrome 6 etc. Not really "recently" in web terms.
- jimmaswell 6y agoYou can mess with private fields with reflection in other languages too. If someone's bent on bypassing your API you're not going to stop them.
- winstonewert 6y agoYou didn't have language enforced immutability. But you still could have convention enforced immutability. If you don't provide apis that mutate your objects and document that mutating is not supported, you get almost all of the benefits of immutability.
- hatch_q 6y agoI don't think mutability is JS design flaw. It was designed for manipulating UI - computer screen, which is mutable. If it was designed for printing (an immutable interface) i'd agree with you. Bigger problem is that people started using javascript for server side, for which is (was) totally unfit for.
- dragonwriter 6y ago> I don't think mutability is JS design flaw. It was designed for manipulating UI - computer screen, which is mutable. You don't need, or particularly benefit from, language-level mutability to specify the next state of UI as a function of previous state plus new inputs, which is really the best way to model interactivity.
- throwaway189262 6y agoJS just has a very simple, flawed design. It's still a rather bad language from a design perspective. One of the shortcuts is that all objects are hashmaps. Fully mutable hashmaps with string keys. You can do this: obj.prop = "hi"; obj["prop"] = "bye"; And you set the same field. And you can do that to any object, reassigning any field type. Most languages have static number of fields in an object, or at least static types for fields already created. Another huge shortcoming is prototype chain based inheritance. Someone can rewrite a random link in your chain and suddenly you have another object type entirely. It's also very bad for performance. Essentially, the inheritance tree of an object can change at runtime.
- hombre_fatal 6y agoThese are more like footguns for beginners than intractable obstacles. For example, you can avoid the issues in your comment with static analysis (Typescript). And the Javascript professional just avoids things like arbitrary obj[prop] and prototype chain hacking. And since they aren't intractable, and smart people regularly decide that it's worth the downsides, you're making the uninteresting observation that $lang has warts.
- chasd00 6y agojavascript gets a lot of heat from folks wishing it was a different language. It's just different, works differently, does things atypical of most languages. You can fight it or learn it.