Simulating The Monocle Mustache In ES5

# Andrea Giammarchi (13 years ago)

This came out of disabling "use strict"; everywhere thread esdiscuss/2013-January/028511

After a better analysis, I have realized it is possible to simulate the monocle mustache in ES5 via "use strict";

Any thought on this, appreciated.

function With(o) {
  return function (f) {
    return Function(
      "with(this){return(" + ("" + f).replace(
        "{", "{'use strict';"
      ) + ").call(this)}"
    ).call(o);
  };
}

With({test: 123})(function () {
  alert(test); // 123
  outer = 456; // throws outer is not defined
});

alert(With({test: 123})(function () {
  return test;
}));