A contingent in the JavaScript community is pretty much solely focused on securing the language. Security is always important, but even more-so when you are dealing with a language that runs in a user agent to run the Web.
Whenever you try to add a feature to a language you are instantly dealing with legacy and backwards compatibility.
Perl has a strict mode that is very popular in the community. I remember projects where if I checked in a module without use strict;
and -w
I would get beaten up.
Back to JavaScript, how can we apply stricter rules on what we do in the JavaScript runtime after the face? One solution that the ECMAScript Edition 5 folks came up with is borrowing use strict
but since they couldn’t add it to the language itself, they have you put it in a string "use strict";
. You can place it at the top of the file, or even as the first statement in a function.
I find it incredibly ugly, yet pragmatic. It works.
However, then I saw some new items:
"use strict,cajita";
And currently you can’t switch the order around? It looks for absolute keys? That seems a little crazy. Surely all of these should be possible:
"use cajita,strict"; "use strict,cajita"; "use strict"; "use cajita";
Then this little pragmatic hack starts to get real messy. I start to joking imagine a ludicrous world like this:
(function() { "use strict"; "use let"; // let's get let in there! "use cramda"; // I love Alex's cramda, let me use it! "use e4x"; // XML inline. Screw templates // If I see a var with the same name, REALLY use let instead "let x;" var x; // Now I can use the cramda "x = #(a, b) { "\ " ... logic here ... "\ "};" // And the let... ironic that it is in a string now "let y = <xml></xml>"; // Annotations come to JavaScript! "@Serializable" function someThing() {} })();
I like to think of myself as pragmatic, but why do I feel so cranky about "use strict";
? :)
May 26th, 2009 at 10:03 am
Totally agree. I’m hoping this is a short-term remedy. Let’s all hope the current generation of browsers is replaced a lot faster than the previous generation! (so it really can be a short-term remedy)
May 26th, 2009 at 11:33 am
OMG that’s just not right…
May 26th, 2009 at 12:08 pm
I’ve read about “use strict” but not about any of the others. Where’d you see “use cajita” for instance?
@unscriptable: All browsers are note being replaced with faster engines. Let me present to you exhibit IE8. It’s faster than IE7 but still slower than every (literally every single solitairy) browser out now.
May 26th, 2009 at 2:18 pm
I wonder what exactly is the point of going through all this trouble to avoid syntax errors, if programs won’t actually be backward-compatible anyways… (I mean, who in their right mind would code things twice like “let x;”;var x; ?)
May 26th, 2009 at 2:52 pm
You know, what you need is something like an XML DOCTYPE for Javascript, that puts the engine into strict mode, but doesn’t open the door to the kind of abuse you’re talking about…
June 2nd, 2009 at 2:35 pm
You’re not adding extra capabilities to the language, you’re only asking the parser to be stricter with its rules. It’s a debugging aid.