6

I'm trying to figure out whether the definition of 'use strict' extends to the prototype methods of the constructor. Example:

var MyNamespace = MyNamespace || {};

MyNamespace.Page = function() {

    "use strict";

};

MyNamespace.Page.prototype = {

    fetch : function() {

        // do I need to use "use strict" here again?

    }

};

According to Mozilla you can use it as:

function strict(){

    "use strict";

    function nested() { return "And so am I!"; }

    return "Hi!  I'm a strict mode function!  " + nested();

}

Does it mean that prototype methods inherit strict mode from the constructor?

Spencer Mark
  • 5,263
  • 9
  • 29
  • 58
  • In a hurry, haven't read this in a while, so I don't know if it has an answer. But seems relevant: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ – Paul Jun 04 '14 at 10:35
  • 1
    You might want to look at [What does "use strict" do in JavaScript, and what is the reasoning behind it?](http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) Basically `"use strict"` is scoped like ordinary variables within a function. – Qantas 94 Heavy Jun 04 '14 at 10:36
  • 3
    AFAIK `"use strict"` has literal source code scope and has nothing to do with any inheritance model Javascript has for object methods. – deceze Jun 04 '14 at 10:36
  • Strict mode only applies to the function scope and all that is nested within, which is what the Mozilla code snippet is showing. Prototype methods do not fall within the function scope of their constructor, so I doubt it will inherit it. – gpgekko Jun 04 '14 at 10:38
  • 1
    How is this a duplicate? The suggested link mentions strict mode but not in the same context!!! – Spencer Mark Jun 04 '14 at 11:18

1 Answers1

5

No.

Strict mode does extend to all descendant (read: nested) scopes, but since your fetch function is not created inside the constructor it is not inherited. You would need to repeat the directive in each of the prototype methods.

Privileged methods in contrast would be in strict mode when the constructor is in strict mode. To avoid repetition in your case, you can

  • a) make the whole program strict by moving the directive to the first line of the script, or
  • b) wrap your class in a module IIFE, and make that strict:

    … = (function() {
        "use strict";
    
        function Page() {
            // inherits strictness
        }
        Page.prototype.fetch = function() {
            // inherits strictness
        };
        return Page;
    }());
    
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Clear, concise and suggests a good solution. Only thinkg I might have done differently is remove the namespace. Also, worth mentioning that the suggested solution is commonly used in languages that compile to JavaScript like CoffeeScript and TypeScript. – Benjamin Gruenbaum Jun 04 '14 at 11:02
  • Whar do you mean by "remove the namespace"? For the explanation only? Feel free to edit the code, I'd just revert if I don't like it ;-) – Bergi Jun 04 '14 at 11:07