beoliver
beoliver

Reputation: 5759

how to reference a method javascript?

Is it possible to reference methods using function calls?

I thought I could try something like this:

function map(f,lst) {
// calling map method directly is fine.
    return lst.map(f)
}

function mapm(m,lst) {
// where m is a passed method
    return map( function(x) { return x.m() }, lst)
}

var list_a = [ [1,9],[2,8],[3,7],[4,6] ]
var list_b = mapm(pop,list_a)

>Uncaught ReferenceError: pop is not defined 

Upvotes: 0

Views: 193

Answers (2)

Nathan Wall
Nathan Wall

Reputation: 10734

You can use Function.prototype.call.bind to create a functional version of a method. This is referred to as "uncurrying this".

function map(f, lst) {
// calling map method directly is fine.
    return lst.map(f)
}

function mapm(m,lst) {
// where m is a passed method
    return map( function(x) { return m(x) }, lst)
}

var pop = Function.prototype.call.bind(Array.prototype.pop);

var list_a = [ [1,9],[2,8],[3,7],[4,6] ]
var list_b = mapm(pop, list_a)

If you need it to work in ancient browsers, you'll need to shim in bind:

if (!Function.prototype.bind) {
  Function.prototype.bind = function (oThis) {
    if (typeof this !== "function") {
      // closest thing possible to the ECMAScript 5 internal IsCallable function
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
    }

    var aArgs = Array.prototype.slice.call(arguments, 1), 
        fToBind = this, 
        fNOP = function () {},
        fBound = function () {
          return fToBind.apply(this instanceof fNOP && oThis
                                 ? this
                                 : oThis,
                               aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}

Upvotes: 1

user578895
user578895

Reputation:

Try:

mapm( 'pop', list_a )
...
return x[ m ]();

If you really want to reference the function itself:

mapm( list_a.pop, list_a ); // or Array.prototype.pop
...
return m.apply( x );

Upvotes: 4

Related Questions