Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function pointer with argument as parameter in a function

Not sure if the title is worded correctly, or if there is a better way of saying it, but I think its okay.

At any rate, I understand the following thus far:

a.b("a", "b", "c", foo);

Where "foo" is a function defined elsewhere that takes in no arguments would simply result in the function a.b() running, with the above parameters. The parameter "foo" can then be called inside function a.b() as simply "foo()". In other words, I understand the above call to be using a function pointer as an argument in the function a.b as a parameter.

Okay, now here is what I'm trying to do...

I want to be able to do something similar to the above, except this time I want foo to have a paramater passed in that argument as follows:

a.b("a", "b", "c", foo("bar"));

Now here is the problem. This will literally result in the paramaters "a", "b", "c" and the result of foo("bar") being used. I don't want this. I want foo("bar") to literally be passed in so that in the function a.b which would look like this (as the header):

a.b(first, second, third, fourth);

Could reference and call the fourth parameter as:

fourth();

Even if "fourth" has an argument in it. I can't seem to find a way around this problem, any advice?

Thanks!

like image 478
Jordan Johns Avatar asked Nov 30 '12 03:11

Jordan Johns


People also ask

How can we pass a pointer as an argument to a function?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Can I pass a value or a variable as a parameter in JavaScript?

It's always pass by value (even when that value is a reference...). There's no way to alter the value held by a variable passed as a parameter, which would be possible if JavaScript supported passing by reference.

Can you use a function as a parameter of another function in JavaScript?

Functions in the functional programming paradigm can be passed to other functions as parameters. These functions are called callbacks. Callback functions can be passed as arguments by directly passing the function's name and not involving them.

Can a function take an object as argument in JavaScript?

In JavaScript, you can use functions as values, just like numbers, strings, and objects. That means you can pass them as arguments, return them from other functions, and set them as properties of objects.


2 Answers

Use an anonymous function to wrap your foo call.

a.b("a", "b", "c", function() {return foo("bar");});

If you need to retain the this value that would be given, you can invoke it using .call. You can also pass along any arguments given.

a.b("a", "b", "c", function(arg1, arg2) {return foo.call(this, "bar", arg1, arg2);});

And of course the function doesn't necessarily need to be anonymous. You can use a named function just as well.

function bar(arg1, arg2) {
    return foo.call(this, "bar", arg1, arg2);
}
a.b("a", "b", "c", bar);
like image 196
I Hate Lazy Avatar answered Sep 16 '22 14:09

I Hate Lazy


It's really easy to use BIND with function

a.b("a", "b", "c", foo.bind(undefined, "bar"));

So when you call foo() inside your function it already has the first binded argument. You can apply as more argumenst as you wish using bind.

Note that bind allways apply arguments to function and place them first. If you want to apply to the and use this:

    if (!Function.prototype.bindBack) {
    Function.prototype.bindBack = function (_super) {
        if (typeof this !== "function") 
            throw new TypeError("Function.prototype.bindBack - can not by prototyped");

    var additionalArgs = Array.prototype.slice.call(arguments, 1), 
        _this_super = this, 
        _notPrototyped = function () {},
        _ref = function () {
            return _this_super.apply((this instanceof _notPrototyped && _super) ? this : _super, (Array.prototype.slice.call(arguments)).concat(additionalArgs));
        };

    _notPrototyped.prototype = this.prototype;
    _ref.prototype = new _notPrototyped();

    return _ref;
  }
}

function tracer(param1, param2, param3) {
console.log(arguments)
}

function starter(callback) {
callback('starter 01', 'starter 02')
}
// See here!!!
// function starter call 'calback' with just 2 params, to add 3+ params use function.bindBack(undefined, param, param, ...)                
    starter(tracer.bindBack(undefined, 'init value'));

See example http://jsfiddle.net/zafod/YxBf9/2/

like image 40
Roman Avatar answered Sep 19 '22 14:09

Roman