Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
“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

2,235 views

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
  • Be the first to comment

  • Be the first to like this

"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>

×