• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
"strict mode": (not) everything you ever wanted to know about JavaScript's strict mode
 

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

on

  • 1,557 views

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

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.

Statistics

Views

Total Views
1,557
Views on SlideShare
1,554
Embed Views
3

Actions

Likes
0
Downloads
1
Comments
0

2 Embeds 3

http://www.linkedin.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • ( 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 'undefined' get defined as such for the sake of old IE versions?)
  • But this isn't much of a controversy? - - restrictions on eval() are a blessing
  • But this might get some hackles up… There's that whole bit about `this` being "broken" in JavaScriptBut if you're expecting `this` to scope up to `window`… now you have broken code
  • Firefox 4 will complain:'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
  • it's opt-in – so you'd have to decide to use itAnd anyway it's just enforcing certain coding styles that you ought to be doing anywayAnd/but you're already linting right?And/but there is an argument to be made for NOT using it

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

  • “strict mode”
    (Not) Everything You Ever Wanted to Know About JavaScript's Strict Mode
  • Just What is this so-called “strict mode”?
    What can it do for me? …or TO ME?
  • “Strict Mode” is…
    • 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
  • Sometimes known as…
    (colloquially, of course)
  • “Crockford Mode”
  • What about some concrete Examples?
  • "strict mode";
    // and then the rest
    // but be careful when
    // concatenating!
    // "more compatible" way:
    (function(){
    "use strict";
    // and then the rest
    })();
    How do I invoke it?
  • // "old" way
    foo = 'is now global';
    "use strict";
    bar = 'is not global';
    // throws a ReferenceError
    Won’t let you do dumb things:implicit globals
  • // "old" way
    foo = {
    bar: function(){},
    baz: 'name'
    };
    with(foo){
    bar(baz);
    }
    "use strict";
    foo = {
    bar: function(){},
    baz: 'name'
    };
    with(foo){
    bar(baz);
    }
    // ERROR!
    Won’t let you do dumb things:WITH(){}
  • …and other things you know better than to try and do in the first place.
    "use strict";
    delete undeclaredVariable; // ERROR!
    var foo = { eval: function(val){} }; // ERROR!
    var arguments = somethingGlobal; // ERROR!
    varNaN = 'not a number'; // ERROR!
    var Infinity = (getAllNaturalNumbers() / 2); // ERROR!
    var undefined = 'undefined'; // ERROR!
  • // "old" way
    eval("that = 'this and that!';");
    console.log('that is...:', that);
    "use strict";
    eval("that = 'this and that!';");
    console.log('that is...:', that);
    // ERROR!
    Not without controversy
  • // "old" way
    (function(){
    console.log(
    'this is...:',
    this
    );
    })();
    // this == window
    "use strict";
    (function(){
    console.log(
    'this is...:',
    this
    );
    })();
    // this == undefined
    Not without controversy
  • Not without controversy
    "use strict";
    function factorial(num){
    if (num <= 1) {
    return 1;
    } else {
    return num * arguments.callee(num-1);
    }
    }
    // ERROR!
  • What does it mean for me?
  • What does it mean for me?
    (probably nothing)
  • Read More
    • Mark Miller, WaldemarHorwat, and Mike Samuel: “Changes to JavaScript, Part 1: EcmaScript 5”
    • http://ajaxian.com/archives/ecmascript-5-changes-to-javascript
    • Video: http://www.youtube.com/watch?v=Kq4FpMe6cRs
    • Angus Croll: “JavaScript Strict Mode”
    • http://javascriptweblog.wordpress.com/2011/05/03/javascript-strict-mode/
    • MDC: ECMAScript 5 support in Mozilla
    • https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla
    • Douglas Crockford: “Strict Mode is Coming to Town”
    • http://www.yuiblog.com/blog/2010/12/14/strict-mode-is-coming-to-town/