6 ms·
A Strong Mode for JavaScript
- msoad 12y ago> It is a SyntaxError to use the keyword 'var' What if I want a variable?!
- jongleberry 12y agouse `let ` or `const `. there's no need to use `var` in ES6+, ever.
- bluesnowmonkey 12y agoBut we're going to keep it around, aren't we? Because we can always add features but we can't really remove them. It feels like JS is getting cruftier. Would it be better if var already worked like let? Sure. Will JS be better with two slightly different ways of scoping a variable, one of which you really shouldn't use? Doesn't seem like it. One more thing to explain to newcomers.
- thomasfoster96 12y agoWell, for newcomers we can either bring them up using `let` exclusively or the explanation of function scope can include a footnote on `let` versus `var`.
- reissbaker 12y agoSo, you're right that nothing can be removed. But you can add new things, and via mode switches (e.g. "use strict", which enables strict mode currently, or "use strong", which is planned to enable strong mode) essentially remove old ones by disallowing them in the latest "mode." Old code will continue to run, but as long as you opt-in to the new way of doing things for your new code you can enforce not using the legacy constructs in the new code. It's important to note that the modes are on a per-function basis, so opting into the new mode doesn't break existing libraries, etc even if you call those libraries from a newer mode. It's also worth mentioning that strong mode doesn't introduce anything new. All that strong mode does is remove features. `let` and `var` both already exist; in strong mode, only `let` does.
- lmm 12y agoHopefully we can tell newcomers to "use strict", always use "let", and to not worry about reading code that doesn't "use strict" to start with? People who are new to FORTRAN get told "if it doesn't say "implicit none" at the top then don't try to read it yet".
- thalesmello 12y agoSo, if I want get the sum of an array of numbers, the only option is to use recursion or a reduce function? Is there a way to accomplish this using a for loop while sticking to let keywords?
- Skalman 12y agoPerhaps you didn't catch that basically the difference between let and var is that let is block-scoped, while var is function-scoped? let result = 0; for (let i = 0; i < arr.length; i++) { result += arr[i]; }
- thomasfoster96 12y agoIt is a SyntaxError to use the identifier ‘eval’. SyntaxError? "use strong"; should probably do something like: let eval = void 0; rather than making eval a SyntaxError.
- pavpanchekha 12y agoMaking it a syntax error means code will not fail quietly. This should help transition code to Strong mode.
- thomasfoster96 12y agoWouldn't a `ReferenceError` make more sense?
- dustin1114 12y agoI think I like most of what this document proposes except for the following: In strong code, accessing objects (strong or not) throws on missing properties. New object properties have to be defined explicitly and cannot be removed from strong objects. To me, this seems to break a fundamental aspect of the language. I've found it very acceptable to be able to define an object literal property "on the fly." However, with strong mode trying to make the language friendlier to eventually being more statically typed, I see the necessity. It just boggles my dynamically typed mind :-)
- TazeTSchnitzel 12y agoBeing able to do foo = bar.x || 3; rather than foo = bar.hasOwnProperty('x') ? bar.x : 3; is nice.
- dustin1114 12y agoAgreed. And unless I'm reading the document wrong, this would also be prohibited in strong mode: let x = { keyA: "valueA" }; x.keyB = "valueB";
- aeosynth 12y agoI think they're trying to push onto [Maps]. [Maps]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- reissbaker 12y agoYep, they're explicitly trying to do that. For what it's worth, you can still use "options hashes" in your argument APIs in strong mode using objects; you just write a library function something like: function options(options, defaults) { // the final args start off as a clone of the default args let args = Object.clone(defaults); // we then loop through the keys and copy in any overrides for(let key of Object.keys(args)) { // ignore inherited properties and skip missing ones if(args.hasOwnProperty(key) && options.hasOwnProperty(key)) { args[key] = options[key]; } } // args now has all of the overrides from options return args; } And then in all your functions that take options objects: function bakeBread(ingredientOverrides) { let ingredients = options(ingredientOverrides, { flourType: 'whole wheat', sugarAmount: '3 tbsp', waterAmount: '1 cup', milkAmount: '0.3 cups', flourAmount: '4 cups' }); let batter = mix(ingredients); return bake(batter); } JS is still quite dynamic, even in strong mode — you can define arbitrary objects and types at runtime, and easily inspect/reflect on them — it's just a little harder to silently corrupt data. The neat thing about using named arguments with objects is that in typed variants of JS — for example, TypeScript, or perhaps someday SoundScript — you can actually typecheck them! Maps can't do that in any language I know of: by design they can contain anything.
- Corporate_Shill 12y agoStill trying to sell Javascript? I guess it's getting harder to find cheap engineers willing to put up with it.
- CmonDev 12y agoYes, I see so many JS-only front-end job posts these days - nobody wants to touch that crap even with a stick: "let's hire a guy who will only do JS, close him in a separate room, and forget about this scripty language like the horror dream it is".
- leichtgewicht 12y agoFirst: this document should be on github. How do I do a PR on a google doc? My biggest problem with this proposal is that it will force a full-blown code-style on you. Many, perhaps most, of the features will be part of the common consent how to write good apps but I am sure lots of people have a problem with feature x or y. I wonder if a "selecting a subset" would be practicable.
- acjohnson55 12y agoMy biggest problem with this proposal is that it will force a full-blown code-style on you. Yes, just like "use strict" did, and thank goodness! The fact of the matter is that Javascript wasn't a carefully designed language from top to bottom and we're still working to fix some of its pitfalls. It's not that some of the ill-posed features don't have valid uses; they do. But we know a lot about the best practices, and with ES6, there are objectively safer alternatives to old patterns. This isn't just for today's experienced JS dev. Javascript is here to stay, and there's no reason the person learning it today should have to know all the language quirks I had to work around. "use strong" will lead to more performant and more resilient JS coding practices, period. Let's stop fetishizing the missteps of the original language.
- noiv 12y agoI don't get this approach, it's backwards. Why can't the JIT assume everything is perfect and spit out information like: Warning: Can't optimize function foo in bar.js because of foobar? Instead devs have to tell the JIT what to assume and wait for errors. What I'm missing here?
- colinramsay 12y agoThis would make more sense to me, if not only because it seems like a great way to avoid the most complained-about parts of JavaScript, like weird type coercion.
- sidekick 12y agoAs someone who is saddened to see the relatively low acceptance of Dart, I hope to more efforts like Strong mode take off. More and more mission critical software is being written in javascript, affecting the livelihood of ordinary people. I think there is a significant audience that can benefit from a less dynamic, more static javascript.