gilbert / es-papp

A proposal for adding partial application support to JavaScript.
359 stars 12 forks source link

Simplify ES6 polyfill #1

Closed steelbrain closed 8 years ago

steelbrain commented 8 years ago

Compiled output before

Function.prototype.papp = function () {
  for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }

  var fn = this;
  return function () {
    for (var _len2 = arguments.length, moreArgs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      moreArgs[_key2] = arguments[_key2];
    }

    fn.call.apply(fn, [this].concat(args, moreArgs));
  };
};

Compiled output after

"use strict";

Function.prototype.papp = function () {
  for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }

  var fn = this;
  return function () {
    for (var _len2 = arguments.length, moreArgs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      moreArgs[_key2] = arguments[_key2];
    }

    fn.apply([this].concat(args, moreArgs));
  };
};

Benefits: Instead of having to do fn.call.apply(fn, ...args), we just directly do fn.apply(...args)

gilbert commented 8 years ago

Thank for the PR! Does apply take an array as a first parameter? I always thought the array came second, after the this value.

fn.apply([this, ...args, ...moreArgs]);
// vs
fn.apply(this, [...args, ...moreArgs]);
steelbrain commented 8 years ago

Good catch, fixed

gilbert commented 8 years ago

Great, thanks!