Simulating The Monocle Mustache In ES5

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Jan 30 10:05:52 PST 2013


This came out of disabling "use strict"; everywhere thread
https://mail.mozilla.org/pipermail/es-discuss/2013-January/028511.html

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;
}));

br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130130/807a73aa/attachment.html>


More information about the es-discuss mailing list