Can I disable ECMAscript strict mode for specific functions?

53,887

Solution 1

No, you can't disable strict mode per function.

It's important to understand that strict mode works lexically; meaning — it affects function declaration, not execution. Any function declared within strict code becomes a strict function itself. But not any function called from within strict code is necessarily strict:

(function(sloppy) {
  "use strict";

   function strict() {
     // this function is strict, as it is _declared_ within strict code
   }

   strict();
   sloppy();

})(sloppy);

function sloppy(){
  // this function is not strict as it is _declared outside_ of strict code
}

Notice how we can define function outside of strict code and then pass it into the function that's strict.

You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function. Of course, that won't work if "sloppy" functions need to reference variables from within main wrapper function.

Also note that indirect eval — suggested by someone else — won't really help here. All it does is execute code in global context. If you try to call a function that's defined locally, indirect eval won't even find it:

(function(){
  "use strict";

  function whichDoesSomethingNaughty(){ /* ... */ }

  // ReferenceError as function is not globally accessible
  // and indirect eval obviously tries to "find" it in global scope
  (1,eval)('whichDoesSomethingNaughty')();

})();

This confusion about global eval probably comes from the fact that global eval can be used to get access to global object from within strict mode (which isn't simply accessible via this anymore):

(function(){
  "use strict";

  this; // undefined
  (1,eval)('this'); // global object
})();

But back to the question...

You can kind of cheat and declare a new function via Function constructor — which happens to not inherit strictness, but that would rely on (non-standard) function decompilation and you would lose ability to reference outer variables.

(function(){
  "use strict";

  function strict(){ /* ... */ }

  // compile new function from the string representation of another one
  var sneaky = Function('return (' + strict + ')()');

  sneaky();
})();

Note that FF4+ seems to disagree with spec (from what I can tell) and incorrectly marks function created via Function as strict. This doesn't happen in other strict-mode-supporting implementations (like Chrome 12+, IE10, WebKit).

Solution 2

(From http://javascriptweblog.wordpress.com/2011/05/03/javascript-strict-mode/)

(...) Strict Mode is not enforced on non-strict functions that are invoked inside the body of a strict function (either because they were passed as arguments or invoked using call or apply).

So if you setup the error methods in a different file, without strict mode, and then pass them as a parameter, like this:

var test = function(fn) {
  'use strict';
  fn();
}

var deleteNonConfigurable = function () {
  var obj = {};
  Object.defineProperty(obj, "name", {
    configurable: false
  });
  delete obj.name; //will throw TypeError in Strict Mode
}

test(deleteNonConfigurable); //no error (Strict Mode not enforced)

...it should work.

Solution 3

An alternative is simply doing this

var stack;
if (console && console.trace) {
     stack = console.trace();
} else {
    try {
        var fail = 1 / 0;
    } catch (e) {
        if (e.stack) {
            stack = e.stack;
        } else if (e.stacktrace) {
            stack = e.stacktrace;
        }
    }
}
// have fun implementing normalize.
return normalize(stack);
Share:
53,887

Related videos on Youtube

jAndy
Author by

jAndy

Frontend Engineer Public CV: http://careers.stackoverflow.com/jandy Contact: [email protected] Playground: CodePen Remembering that I'll be dead soon, is the most important tool I've ever encountered to help me make the big choices in Life. Because almost everything - all external expectations, all pride, all fear of embarrassment or failure, these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die, is the best way I know to avoid the trap of thinking you have something to lose. You are already naked, there is no reason not to follow your heart. -- Steve Jobs

Updated on September 22, 2020

Comments

  • jAndy
    jAndy almost 4 years

    I don't find anything about my question here on MDC or the ECMAscript specifications. Probably somebody knows a more 'hacky' way to solve this.

    I'm calling "use strict" on every javascript file in my environment. All my files start like this

    (function(win, doc, undef) {
        "use strict";
    
        // code & functions
    }(window, window.document));
    

    Now, I have a custom function which handles errors. That functions uses the .caller property to provide a context stack trace. Looks like this:

    var chain = (function() {
        var _parent = _error,
            _ret = '';
    
        while( _parent.caller ) {
            _ret += ' -> ' + _parent.caller.name;
            _parent = _parent.caller;
        }
    
        return _ret;
    }());
    

    But of course, in strict mode .caller is a non-deletable prop which throws when retrieved. So my question is, is anybody aware of way to disable strict more "function-wise" ?

    "use strict"; is inherited by all functions after it was called. Now we have the possibilty to just use strict mode in specific functions by just calling "use strict"; at the top of those, but is there a way to achieve the opposite ?

    • Raynos
      Raynos about 13 years
      You could hack it with a global eval to bypass strict mode
    • mplungjan
      mplungjan about 13 years
      In case others need to know what this is all about: What does use strict do
    • davin
      davin about 13 years
      I almost hope something like this doesn't exist. The idea of "use strict" is an opt-in strategy for backward-compatibility. I'm not sure I like the idea of forward-backward-compatibility, where we allow code to opt-in and then allow subsections of code to opt-out again.
    • hisland
      hisland almost 8 years
      you can see here >>just change .babelrc solution
  • jAndy
    jAndy about 13 years
    doesn't seem to work for me in FF4. I completely removed strict instructions on the file and it's method where the error handling method is located. I'll still receive strict errors.
  • Szymon Toda
    Szymon Toda over 10 years
    How to pass argument to sneaky function?
  • Juangui Jordán
    Juangui Jordán over 6 years
    Thanks, I had an ugly problem in a jasmine unit test needing to modify a global variable used by the class under test, and I couldn't manage to do it. Awesome cheat!
  • Jeyanth
    Jeyanth almost 5 years
    @kangax, If I'm using strict mode, but I want to disable it dynamically. i.e., I'm declaring a function as strict but at some case, I don't want that function as strict and I want to change the function as non-strict.What i'm supposed to do.?