Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 1.16 KB

2014-01-javascript-invoke-function.md

File metadata and controls

32 lines (24 loc) · 1.16 KB
title date author layout
A JavaScript Invoke Function
2014-01-04 21:00
Blake Embrey
article

Update: Now available on github and npm.

Under certain functional JavaScript toolbelts, we can find a utility that is used purely for invoking a method on a passed in object. The utility is a really simple snippet that can be used in a number of different circumstances.

var __slice = Array.prototype.slice;

var invoke = function (method /*, ...args */) {
  var args = __slice.call(arguments, 1);

  return function (obj /*, ..args */) {
    return obj[method].apply(obj, args.concat(__slice.call(arguments, 1)));
  };
};

The most useful situation for a utility such as this is in combination with other functional utilities and iterators. Consider the case where we have an array of objects with identical methods. Not uncommon in a complex MVC application where you may be tracking child views. To remove every child view, we need to iterate over an array of views and call remove.

var children = [
  /* ... */
];

children.forEach(invoke("remove"));