Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-22-2013, 10:09 AM   PM User | #1
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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.
__________________

nikos101 is offline   Reply With Quote
Old 02-22-2013, 11:36 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,599
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 02-22-2013, 11:49 AM   PM User | #3
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
doesn't that mean that its a pain when you import lots of scripts that you have made to keep functionality in separate files?
__________________

nikos101 is offline   Reply With Quote
Old 02-22-2013, 12:08 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,599
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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
})();
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 02-22-2013, 12:15 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by VIPStephan; 02-22-2013 at 03:30 PM..
Philip M is offline   Reply With Quote
Old 02-23-2013, 06:41 AM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 02-23-2013, 06:44 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 02-23-2013, 09:59 AM   PM User | #8
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,599
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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 
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 02-23-2013, 09:16 PM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 02-23-2013 at 09:20 PM..
felgall is offline   Reply With Quote
Old 02-24-2013, 02:00 AM   PM User | #10
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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):
Quote:
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 02-24-2013, 09:28 AM   PM User | #11
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
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.
Celtboy is offline   Reply With Quote
Old 02-24-2013, 10:30 AM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 02-24-2013 at 10:35 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:50 PM.


Advertisement
Log in to turn off these ads.