Can I disable ECMAscript strict mode for specific functions?

后端 未结 3 1726
轻奢々
轻奢々 2020-11-30 00:21

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

3条回答
  •  醉梦人生
    2020-11-30 00:47

    (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.

提交回复
热议问题