4 ms·
Am I right in thinking that the javascript /* "use strict" */ construct would have caught this mistake (just like it would have done for me in perl code)? S
by mst 15y ago
Am I right in thinking that the javascript
/* "use strict" */
construct would have caught this mistake (just like it would have done for me in perl code)? Seems to me that some such feature is absolutely and utterly necessary in any environment where you're doing a lot of closure creation ..
- apaprocki 15y agoYes: $ node > "use strict"; initial = "foo"; ReferenceError: initial is not defined at repl:1:17 at Interface.<anonymous> (repl.js:168:22) ...
- Raticide 15y agoYeah, that's all he needed. Making it a comment isn't necessary either. Maybe Node should be strict by default? I can understand it not being the case for browsers that need to support legacy code, but for Node it doesn't really need to care about that.
- morsch 15y agoHuh. On the scale from pragmatic idiosyncrasy to simply bizarre, making a string literal expression change the runtime behaviour in that manner is fairly wide towards the bizarre end of the scale. I think, if anything, requiring it to be in a comment would have been less odd; at least comments imply a sense of "meta"-ness.
- apgwoz 15y agoYeah, I always thought the string idea was wacko too, but introducing a new keyword in a language that has to be backwards compatible or suffer doom isn't a good idea either.
- bzbarsky 15y agoComments are commonly stripped by various source code processors out there, including minifiers and some browser JS engine front-ends. The string literal doesn't make anyone _happy_, but it was most compatible with the existing widely deployed toolchains, as I understand.
- kijin 15y agoNot to mention that it reeks of VB6's "Option Explicit" and "Option Strict" statements. I hated those.
- masklinn 15y ago> On the scale from pragmatic idiosyncrasy to simply bizarre, making a string literal expression change the runtime behaviour in that manner is fairly wide towards the bizarre end of the scale. Yes, on the other hand it's quite necessary to not have non-compliant browsers blow up. `"use strict";` is just a noop in e.g. IE6. `use strict;` would be an error in it. > I think, if anything, requiring it to be in a comment would have been less odd; at least comments imply a sense of "meta"-ness. Javascript compressors definitely strip comments. They probably don't strip strings.
- mst 15y agoQuite possibly a good idea. use v5.12; in perl code enables strict, at least.
- alunny 15y agoNode itself (the core js library) isn't strict mode compliant: it uses octal literals for filesystem interactions.
- bmj 15y agoRunning some variation of JSLint in his editor/IDE would likely have caught this, too.
- sjs 15y agoOr using js2-mode in Emacs. That's just one small reason why Emacs in my JavaScript workhorse. I use TextMate for most things but if I'm writing JS all day nothing beats Emacs.
- geraldalewis 15y agoYes, though never enable strict mode (in JS) unless you know exactly what it does. `use strict` causes some subtle semantic shifts that can be easily overlooked. For example, there's no dynamic binding between the `arguments` object and a function's arguments: ((x)-> x = 2; arguments[0] is 1)(1) # true ((x)-> arguments[0] = 2; x is 1)(1) # true Any developer enabling strict mode should be testing heavily in browsers that actually support the feature. There have been a few high-profile meltdowns already (on BoA, Intel, eBay, etc.) due to devs not understanding the feature, or concatenating a `"use strict"` script (not wrapped in an IIFE) with non-strict code. JSLint doesn't help matters, as it errors unless `"use strict"` is declared (unless run with the "sloppy" option -- something I'm sure devs love). The absolute worst scenario is for a browser to implement some kind of heuristics-based approach to respect the `"use strict"` prologue; what Brendan Eich calls a "strict quirks mode". All that said, I'm a big fan of `strict`. It might be a good idea to use it in production, and strip the directives out when deploying.
- ErikD 15y agoSince the code in question was run server side using nodejs you can just enable it. Using it for client side javascript is another case indeed.
- deleted 15y ago[deleted]
- chrisrhoden 15y agoNo. > var initial = 'string'; > "use strict"; (function(){ initial = 'anotherstring' })(); > initial 'anotherstring' >