Skip to content

contextify

Subhajit Sahu edited this page Jul 29, 2022 · 2 revisions

Contextify a function by accepting the first parameter as this-object.

Similar: contextify, decontextify.


function contextify(x)
// x: a function

const xfunction = require('extra-function');


function count(x, value) {
  var a = 0;
  for (var v of x)
    if (v===value) ++a;
  return a;
}

var fn = xfunction.contextify(count);

fn.call([6, 3, 4, 3], 3);
// → 2

fn.call([6, 1, 4, 3], 3);
// → 1
Clone this wiki locally