Skip to content

Instantly share code, notes, and snippets.

@yefremov
Created March 7, 2017 02:39
Show Gist options
  • Save yefremov/4e95e362af8ad2b2bf94e90aba52db56 to your computer and use it in GitHub Desktop.
Save yefremov/4e95e362af8ad2b2bf94e90aba52db56 to your computer and use it in GitHub Desktop.

Revisions

  1. yefremov created this gist Mar 7, 2017.
    24 changes: 24 additions & 0 deletions uncurryThis.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@

    // How does one write a JavaScript meta-program that can operate reliably on
    // other objects in its JavaScript context (in a browser – within the same
    // frame), despite the loading of arbitrary code later into that same frame?
    // The problem is that the later code may arbitrarily modify, override, or
    // remove methods on the primordial built-in objects. To make the problem
    // tractable, we assume that the meta program consists of modules that are
    // first evaluated before their frame has become corrupted.

    function uncurryThis(func) {
    return function () {
    return Function.call.apply(func, arguments);
    }
    }

    var slice = uncurryThis(Array.prototype.slice);
    var reduce = uncurryThis(Array.prototype.reduce);
    var map = uncurryThis(Array.prototype.map);
    var filter = uncurryThis(Array.prototype.filter);
    var some = uncurryThis(Array.prototype.some);
    var every = uncurryThis(Array.prototype.every);

    console.log(slice(['foo', 'bar', 'baz'], 1, 2));
    // => ["bar"]