Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts

    Question the usage of function x() contained in foo is undefined in strict mode.

    how come when I use
    "use strict";
    in a script (foo.js), then in my html page, that imports foo.js, the usage of function x() contained in foo is undefined.


  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,479
    Thanks
    6
    Thanked 981 Times in 954 Posts
    Probably because it’s not defined.
    That means, it is not defined within the scope of where you are calling it. In strict mode, the use of global variables is forbidden, if I recall correctly, so special attention is needed in regards of the scope of variables and functions used.

  • #3
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    doesn't that mean that its a pain when you import lots of scripts that you have made to keep functionality in separate files?


  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,479
    Thanks
    6
    Thanked 981 Times in 954 Posts
    That’s why strict mode can (and is recommended to) be applied on a function-by-function basis rather than globally. What you can do for scope’s purposes is to put all your scripts into a self-invoking anonymous function so you can create your own local “global” scope:
    Code:
    (function() {
      "use strict";
      // your JS here; strict mode is only applied within this function
    })();

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by nikos101 View Post
    doesn't that mean that its a pain when you import lots of scripts that you have made to keep functionality in separate files?
    Yep. It is likely to break existing older scripts. I would suggest to use "use strict" only where you need to do so.

    In any case IE does not support "use strict" below version 10. I suggest only using "use strict" for debugging in Chrome and afterwards delete it again!

    You can use strict for only specific functions rather than the whole script.:

    Code:
    // Non-strict code...
    
    (function() {
      "use strict";
    
      // Define your function strictly...
    })();
    
    // Non-strict code...
    See also http://www.howtocreate.co.uk/strictJSFirefox.html

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by VIPStephan; 02-22-2013 at 03:30 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by VIPStephan View Post
    Probably because it’s not defined.
    That means, it is not defined within the scope of where you are calling it. In strict mode, the use of global variables is forbidden, if I recall correctly, so special attention is needed in regards of the scope of variables and functions used.
    how would you refer to anything if globals were forbidden?
    use strict is a weaker sub-set of full JS, but i don't think it's that limited...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by nikos101 View Post
    how come when I use
    "use strict";
    in a script (foo.js), then in my html page, that imports foo.js, the usage of function x() contained in foo is undefined.
    does code in foo.js call the eval or Function function, or use with?

    while no issue for normal JS, using the strict subset turns these normal statements into syntax errors, which would prevent all the functions in foo.js (including x) from showing up in the HTML page's environment.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,479
    Thanks
    6
    Thanked 981 Times in 954 Posts
    Quote Originally Posted by rnd me View Post
    how would you refer to anything if globals were forbidden?
    use strict is a weaker sub-set of full JS, but i don't think it's that limited...
    Well, I might have expressed myself not clearly enough. Variables have to be defined as such before they can be used, i. e. global variables are not implied.
    PHP Code:
    "use strict";
    example1 'whatever'// wrong; missing “var” keyword
    var example2 'I don’t care'// correct 

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by Philip M View Post
    Yep. It is likely to break existing older scripts. I would suggest to use "use strict" only where you need to do so.
    WRONG. "use script" does NOT break the code in browsers that don't support it - it is a valid statement even in Netscape 2 just as long as it is enclosed in quotes. It just doesn't actually do anything in older browsers as they interpret it as creating a string and then not doing anything with it.

    The whole point in introducing it into JavaScript is so that people can convert parts of pages from 20th Century JavaScript into the latest version of JavaScript. Had it not been introduced then old scripts would break in modern browsers because they do not comply with the latest JavaScript standards.

    The only time you should omit "use strict" is where the script has yet to be rewritten to use the latest version of JavaScript. All new scripts should use it as why would you use an old version of JavaScript rather than the latest 2011 standard.

    As VIPStephan said you should be applying it as the first statement inside of the anonymous functions that modern JavaScript uses to keep scripts separated from one another so that they don't try to put variables into global scope that will be overwritten by other scripts in the page. Any antiquated scripts in the same page will then still work as they do not have the command that identifies that they are using the latest version of JavaScript.

    The new version of JavaScript has deleted some useless and problematic commands from JavaScript (eg. with) started tightening up the security on other commands that have very limited use (eg. eval) and introduced additional requirements that make finding errors easier (eg. requiring that all variables be declared so that a typo in a variable name produces a syntax error pointing to that entry instead of you spending hours wondering why the script runs but doesn't produce the expected result.

    Most browsers now do support "use strict" as identifying that the code is written to the latest standard and so provided you test in one of those browsers first you should not end up with a script that works in some browsers and not others.
    Last edited by felgall; 02-23-2013 at 09:20 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by felgall View Post
    The whole point in introducing it into JavaScript is so that people can convert parts of pages from 20th Century JavaScript into the latest version of JavaScript. Had it not been introduced then old scripts would break in modern browsers because they do not comply with the latest JavaScript standards.

    The only time you should omit "use strict" is where the script has yet to be rewritten to use the latest version of JavaScript. All new scripts should use it as why would you use an old version of JavaScript rather than the latest 2011 standard.
    how is "use strict" in ES5 any more recent than ES5 itself? that doesn't make sense. eval, with, arguments.callee, Function, etc, those are all defined in the same spec as "use strict".

    what "oold scripts would break in modern browsers" if "use strict" were not added? i can't think of any that work "using strict" that would work without it.
    can you provide an example of what you mean? this is bordering on McCarthyism...

    there is nothing inherently newer or more modern about "use strict" compared to ES5, other than newly limiting your options compared to pre-911 JS.

    don't take our word for it, listen to mdn (updated 2/9/2013):
    Strict mode in browsers

    Browsers don't reliably implement strict mode yet, so don't blindly depend on it. Strict mode changes semantics. Relying on those changes will cause mistakes and errors in browsers which don't implement strict mode. Exercise caution in using strict mode, and back up reliance on strict mode with feature tests that check whether relevant parts of strict mode are implemented. Finally, make sure to test your code in browsers that do and don't support strict mode. If you test only in browsers that don't support strict mode, you're very likely to have problems in browsers that do, and vice versa.
    that sounds like a whole lot of work for relatively little payoff.

    what if the payoff of strict?
    1. potentially performance - not yet, some things are actually slower with strict, and V8/FF optimizes things they used to think couldn't be optimized (like with), negating many future gains strict might have someday provided when drafted.

    2. security - maybe, although viewing a function's source doesn't mean you can probe privates, even if you can see their names. At any rate a function can be safely written in strict or normal using the right semantics.


    3. preventing code mistakes. probably the best use of "use strict". Not as comprehensive as JSLint/JSHint, but not a bad thing in dev environments.
    avoiding implicit globals is a win, but im guess that folks who worry about implementing "use strict" are not making such easily-detectable errors. Since the other lint tools are better, this usage of strict is limited in benefits.


    4. future compat -or "someday JS might remove with(obj)". "use strict" doesn't let you do anything you can't do without it, or anything you can do in ES6. When future JS is out and uptake is sufficient to use it on production, our code is going to need a lot of re-writing to use all the new features. It will not break code then it does arrive. given all that, this seems like premature optimization based on speculation.


    so, all the advantages for "use strict" have yet to really pan out. If i saw a new client that only ran strict, or some impressive before-and-after benchmark numbers, or some V8 optimizations, i might be more inclined to recommend it.

    i will concede that sometimes "use strict" is ok.
    certain low-level utilities that work in strict or regular should probably go-ahead and add "use strict". It won't hurt anything, doesn't take up a lot of space, and maybe one day it will actually run your function faster. In a tightly-controlled environment designed for low-power devices, a global use-strict help prevent some performance killer and memory hogs, but you better have every bit unit tested and freshly coded if that's how your'e gonna roll.


    lastly, one really annoying about all this:
    A lot of newcomers to JS seem to think "use strict" is cool, so if you use it you are cooler in their eyes. It's the new HTML validation. If it validates, then your code is awesome right? It's handy to point to a tiny thing anyone can do and say "it makes it better/ok", but if you look at the costs/benfits of "use strict", it's not a no-brainer.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #11
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    620
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Even better than preventing code mistakes is having *some* level of enforcement of better coding practices. "use strict" gets you in the habit of writing structured, maintainable code that plays better with others.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by felgall View Post
    WRONG. "use script" does NOT break the code in browsers that don't support it - it is a valid statement even in Netscape 2 just as long as it is enclosed in quotes. It just doesn't actually do anything in older browsers as they interpret it as creating a string and then not doing anything with it.
    You have misunderstood me. I was not refering to older browsers. I was referring to older scripts being executing in modern browsers which do support "use strict".

    As we both agree, you can use strict for only specific functions rather than the whole script.

    One more time - In any case IE does not support "use strict" below version 10. I suggest only using "use strict" for debugging in Chrome and afterwards delete it again!

    Browsers don't reliably implement strict mode yet, so don't blindly depend on it.

    It has been pointed out that JSLint will reveal the errors that "use strict" is supposed to overcome.
    Last edited by Philip M; 02-24-2013 at 10:35 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •