Git Product home page Git Product logo

partial-apply's Introduction

partial-apply

Implements a context-free version of Function.prototype.apply in Node.js/JavaScript.

If someFn has unknown arguments, and we want to pass those arguments through to a wrapped function, say someOtherFn, we can use someOtherFn.apply() to call the wrapped function with someFn's arguments.

But in the process, we corrupt the "this" context of the wrapped function. In order to have it stay correctly defined, we have to pass in the context manually. This creates some nasty dependencies and hurts reusability in your code base.

_.partial() in lodash gets us a bit closer, but it does not support an array of arguments- just arguments passed in one after the other, like _.partial(fn,x0,x1,etc).

For example, if we expected someOtherFn in the example below to have its "this" value refer to, say, a Waterline model, it's actually quite annoying in vanilla JavaScript to pass down a dynamic array of arguments to a function AND maintain its original "this" context.

Anyway, this is why partial-apply was created.

Installation

$ npm install partial-apply

Usage

var partialApply = require('partial-apply');

function someFn( /* ... */ ) {
  var fnWithArgsPreApplied = partialApply(someOtherFn, arguments);
  fnWithArgsPreApplied();
}

Why?

This module is a temporary solution, until lodash has something similar in core that can be called as a single method. In the mean time, this is a hack to make our code more readable.

To get the latest status, see the lodash docs.

I'll update this repo with install/version info if something comparable is added to lodash core at some point.

License

MIT © Mike McNeil 2014

partial-apply's People

Contributors

mikermcneil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.