0

I have multiple places in my code where i use method references(i.e. just the method name with no arguments) but I need to pass it specefic arguments.

I don't want to insert an anonymous method b.c. it makes the code unreadable.

I've told I can use the .bind method, but I don't know how to use it properly. Can some one elaborate on how to do this.

Here is one example of where I need to to to this.

How do I use bind to add in parameters to ajax_signin?

if(d===0){ajax('arche_model.php',serialize(c)+'&a=signin',ajax_signin,b);}
7
  • Which method are you wanting to make smaller here? Commented Nov 9, 2011 at 18:21
  • you want to use .bind? or don't want use anonymous methods? Commented Nov 9, 2011 at 18:27
  • I would like to use bind in this instance to add parameters to ajax_signin Commented Nov 9, 2011 at 18:33
  • Are you passing around method names as strings? Or are you passing around the methods themselves? Commented Nov 9, 2011 at 18:34
  • Have you read the tutorial on bind at MDN? developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… They have quite a few examples Commented Nov 9, 2011 at 18:37

2 Answers 2

1

If you want ajax_signin() to get called with parameters, then you have to make a separate function that you can pass to ajax that calls ajax_signin() with the appropriate parameters. There are a couple ways to do this:

Using an anonymous function:

if(d===0){ajax('arche_model.php',serialize(c)+'&a=signin',function() {ajax_signin("parm1","parm2")},b);}

Creating your own named function:

function mySignIn() {
    ajax_signin("parm1","parm2");
}

if(d===0){ajax('arche_model.php',serialize(c)+'&a=signin',mySignIn,b);}

If you want to use .bind() and you are sure you are only running in browsers that support .bind() or you have a shim to make .bind() always work, then you can do something like this:

if(d===0){ajax('arche_model.php',serialize(c)+'&a=signin',ajax_signin.bind(this, "parm1","parm2"),b);}

The .bind() call creates a new function that always has a specific this ptr and always has "parm1" and "parm2" as it's first two parameters.

Sign up to request clarification or add additional context in comments.

7 Comments

But there should be a way to use bind?
This is not a good answer, there is a general use answer using partial function application and to manually do this each time would be archaic.
I added a .bind() option to my answer, though I'm not sure how it's any better in this case than the anonymous function option and .bind() has browser compatibility issues that you have to plan for.
@GregAgnew - if you have the same parameters you want to send over and over in multiple places, you simply make one named function and use that everywhere. .bind() is new and not universally supported and doesn't seem like it saves much for this purpose. Your partial solution hardly seems simpler. I don't see how that's any simpler than creating either a named function or an anonymous function. The OP can now choose from the three different solutions offered.
@jfriend00 .bind is only supported in newer browsers, .apply is supported way back. The bigger difference however is that partial allows you to apply a context to the function in an elegant way. The only way for you to do this would be function() {ajax_signin.apply(context,"parm1","parm2")}; Which I think is not very elegant.
|
0

You should be using partial function application! IE: The following:

// This will call a function using a reference with predefined arguments.
function partial(func, context /*, 0..n args */) {
  var args = Array.prototype.slice.call(arguments, 2);
  return function() {
    var allArguments = args.concat(Array.prototype.slice.call(arguments));
    return func.apply(context ? context : this, allArguments);
  };
}

The first argument is the function you want to call, the second argument the context, and any arguments after that will be 'pre-loaded' into the function call.

Note: 'Context' is what this will refer to once the function is being executed.

3 Comments

This is similar to what .bind() does. I don't see how it's simpler or more efficient than an anonymous function declaration which has been a tried and true staple of Javascript development for a long time or better than what .bind() does. I'm not saying it won't work - I just don't understand why it's better?
It is a matter of preference..I think..+ I just want to learn
Its better beacuse .bind is not supported in older browsers. It is from javascript 1.8.5 where as apply goes back to 1.3 developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.