0
“strict mode”<br />(Not) Everything You Ever Wanted to Know About JavaScript's Strict Mode<br />
Just What is this so-called “strict mode”?<br />What can it do for me? …or TO ME?<br />
“Strict Mode” is…<br /><ul><li>Introduced as part of ECMA-262 (5th Ed.)
Quickly becoming an available option in major browsers
“A slight augmentation” of how ECMAScriptengines interpret/execute code
“Like a seatbelt for your code” in that it tries to protect you from yourself</li></li></ul><li>Sometimes known as…<br />(...
“Crockford Mode”<br />
What about some concrete Examples?<br />
"strict mode";<br />// and then the rest<br />// but be careful when<br />// concatenating!<br />// "more compatible" way:...
// "old" way<br />foo = 'is now global';<br />"use strict";<br />bar = 'is not global';<br />// throws a ReferenceError<br...
// "old" way<br />foo = {<br />bar: function(){},<br />baz: 'name'<br />};<br />with(foo){<br />bar(baz);<br />}<br />"use...
…and other things you know better than to try and do in the first place.<br />"use strict";<br />delete undeclaredVariable...
// "old" way<br />eval("that = 'this and that!';");<br />console.log('that is...:', that);<br />"use strict";<br />eval("t...
// "old" way<br />(function(){<br />console.log(<br />'this is...:',<br />    this<br />);<br />})();<br />// this == wind...
Not without controversy<br />"use strict";<br />function factorial(num){<br />    if (num <= 1) {<br />        return 1;<b...
What does it mean for me?<br />
Upcoming SlideShare
Loading in...5
×

"strict mode": (not) everything you ever wanted to know about JavaScript's strict mode

1,514

Published on

A 30-minute presentation given at Dealer.com on May 6, 2011.

An overview of what "strict mode" means ECMAScript 5, and what it means (and does not mean) for everyday web development.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,514
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • ( Firefox has had it for quite some time… Chrome and Safari have it now… IE9? )
  • Invoked as either- The first line in a whole JS file –or-- As the first line in a function
  • Enforcing some rules that were common sense… and/or were not exactly built-in but should have been……and/but then we get to the controversy? (who in here has seen &apos;undefined&apos; get defined as such for the sake of old IE versions?)
  • But this isn&apos;t much of a controversy? - - restrictions on eval() are a blessing
  • But this might get some hackles up… There&apos;s that whole bit about `this` being &quot;broken&quot; in JavaScriptBut if you&apos;re expecting `this` to scope up to `window`… now you have broken code
  • Firefox 4 will complain:&apos;caller&apos;, &apos;callee&apos;, and &apos;arguments&apos; properties may not be accessed on strict mode functions or the arguments objects for calls to them
  • it&apos;s opt-in – so you&apos;d have to decide to use itAnd anyway it&apos;s just enforcing certain coding styles that you ought to be doing anywayAnd/but you&apos;re already linting right?And/but there is an argument to be made for NOT using it
  • Transcript of ""strict mode": (not) everything you ever wanted to know about JavaScript's strict mode"

    1. 1. “strict mode”<br />(Not) Everything You Ever Wanted to Know About JavaScript's Strict Mode<br />
    2. 2. Just What is this so-called “strict mode”?<br />What can it do for me? …or TO ME?<br />
    3. 3. “Strict Mode” is…<br /><ul><li>Introduced as part of ECMA-262 (5th Ed.)
    4. 4. Quickly becoming an available option in major browsers
    5. 5. “A slight augmentation” of how ECMAScriptengines interpret/execute code
    6. 6. “Like a seatbelt for your code” in that it tries to protect you from yourself</li></li></ul><li>Sometimes known as…<br />(colloquially, of course)<br />
    7. 7. “Crockford Mode”<br />
    8. 8. What about some concrete Examples?<br />
    9. 9. "strict mode";<br />// and then the rest<br />// but be careful when<br />// concatenating!<br />// "more compatible" way:<br />(function(){<br />"use strict";<br />// and then the rest<br />})();<br />How do I invoke it?<br />
    10. 10. // "old" way<br />foo = 'is now global';<br />"use strict";<br />bar = 'is not global';<br />// throws a ReferenceError<br />Won’t let you do dumb things:implicit globals<br />
    11. 11. // "old" way<br />foo = {<br />bar: function(){},<br />baz: 'name'<br />};<br />with(foo){<br />bar(baz);<br />}<br />"use strict";<br />foo = {<br /> bar: function(){},<br />baz: 'name'<br />};<br />with(foo){<br /> bar(baz);<br />}<br />// ERROR!<br />Won’t let you do dumb things:WITH(){}<br />
    12. 12. …and other things you know better than to try and do in the first place.<br />"use strict";<br />delete undeclaredVariable; // ERROR!<br />var foo = { eval: function(val){} }; // ERROR!<br />var arguments = somethingGlobal; // ERROR!<br />varNaN = 'not a number'; // ERROR!<br />var Infinity = (getAllNaturalNumbers() / 2); // ERROR!<br />var undefined = 'undefined'; // ERROR!<br />
    13. 13. // "old" way<br />eval("that = 'this and that!';");<br />console.log('that is...:', that);<br />"use strict";<br />eval("that = 'this and that!';");<br />console.log('that is...:', that);<br />// ERROR!<br />Not without controversy<br />
    14. 14. // "old" way<br />(function(){<br />console.log(<br />'this is...:',<br /> this<br />);<br />})();<br />// this == window<br />"use strict";<br />(function(){<br />console.log(<br />'this is...:',<br /> this<br />);<br />})();<br />// this == undefined<br />Not without controversy<br />
    15. 15. Not without controversy<br />"use strict";<br />function factorial(num){<br /> if (num <= 1) {<br /> return 1;<br /> } else {<br /> return num * arguments.callee(num-1);<br /> }<br />}<br />// ERROR!<br />
    16. 16. What does it mean for me?<br />
    17. 17. What does it mean for me?<br />(probably nothing)<br />
    18. 18. Read More<br /><ul><li>Mark Miller, WaldemarHorwat, and Mike Samuel: “Changes to JavaScript, Part 1: EcmaScript 5”
    19. 19. http://ajaxian.com/archives/ecmascript-5-changes-to-javascript
    20. 20. Video: http://www.youtube.com/watch?v=Kq4FpMe6cRs
    21. 21. Angus Croll: “JavaScript Strict Mode”
    22. 22. http://javascriptweblog.wordpress.com/2011/05/03/javascript-strict-mode/
    23. 23. MDC: ECMAScript 5 support in Mozilla
    24. 24. https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla
    25. 25. Douglas Crockford: “Strict Mode is Coming to Town”
    26. 26. http://www.yuiblog.com/blog/2010/12/14/strict-mode-is-coming-to-town/</li>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×