Skip to content

contextify

Subhajit Sahu edited this page Aug 7, 2022 · 1 revision

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

Similar: contextify, decontextify.


function contextify(x)
// x: a function

const xasyncfn = require('extra-async-function');


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

var fn = xasyncfn.contextify(count);

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

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