2

I have this simple code :

console.log('calling doWork func...')
doWork('a', 'b', myCb);

function doWork(param1, param2, callback)
{
        console.log('in func')
        callback();
}


function myCb(a)
{
        console.log('in callback... for value ' + a)
}

I'm running the function with a callback function -

The output I get is :

"calling doWork func..."
"in func"
"in callback... for value undefined"    // notice the undefined

All ok.

Now, I want the callback to be called with a specified param - something like ( which is incorrect since the () is executing the func) :

doWork('a', 'b', myCb('xxxx'));

I want the output to be :

in callback... for value  xxxx

How do I send a parameter with the myCb call in doWork('a', 'b', myCb);? ( 'when you run the callback function , please run it according to value xxxx')

any help ?

p.s. I want to avoid any global flags solution

thanks.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • may be duplicated http://stackoverflow.com/questions/1997531/javascript-callback-function-and-parameters – mask8 Jul 25 '12 at 07:01
  • @mask8 please search the term `.bind` in your link , I *don't* see it. so I assume it is not duplicate ( different solutions) – Royi Namir Jul 25 '12 at 07:02
  • how about this one? http://stackoverflow.com/questions/3458553/javascript-passing-parameters-to-a-callback-function there are too much of this questions anyway – mask8 Jul 25 '12 at 07:07
  • @mask8 Ok , Agree. ( didnt find it before though) ...:) – Royi Namir Jul 25 '12 at 07:07
  • @RoyiNamir when that first question was written, ES5 had been approved [less than a month prior](http://www.ecma-international.org/news/PressReleases/PR_Ecma%20approves%20major%20revision%20of%20ECMAScript.htm). At that time, most browsers didn't support it, and most developers weren't familiar with it. The lack of `.bind` in the answers is definitely not suggestive that it is not a duplicate (and *certainly* not conclusive in any case). – apsillers Jul 25 '12 at 07:10

4 Answers4

4

The common solution is to create another function in place.

doWork('a', 'b', function () {
    myCb('xxxx');
});

You can also use a function that would abstract the currying away. JavaScript (ES5) even has one built-in – Function.prototype.bind. Mind you, the native bind will make your callback slow and has limited support in browsers (see the MDN page).

doWork('a', 'b', myCb.bind(null, 'xxxx'));
katspaugh
  • 17,449
  • 11
  • 66
  • 103
  • What are the issue(s) with the native bind? –  Jul 25 '12 at 07:04
  • 1
    @pst, while binding a context, it also allows `call` and `apply` to redefine it dynamically. Try to implement such `bind`, and you'll see. That makes each call to a bound function slow. [jsperf](http://jsperf.com/artificial-vs-native-bind/3) – katspaugh Jul 25 '12 at 07:07
1

One of the solutions is to use .bind:

doWork('a', 'b', myCb.bind(null, 'xxx'));
freakish
  • 54,167
  • 9
  • 132
  • 169
1

Or you could use apply and arguments

console.log('calling doWork func...')
doWork('va', 'b', myCb);

function doWork(param1, param2, callback)
{
        console.log('in func')


        callback.apply(this,arguments);
}


function myCb(a)
{
        console.log('in callback... for value ' + a)
}

JSbin Example

Moritz Roessler
  • 8,542
  • 26
  • 51
0
console.log('calling doWork func...')
doWork (myCb, "a", "b");
function doWork (callback) {
console.log('in func')
callback (arguments[1], arguments[2]);
}
function myCb (param1, param2) {
console.log('in callback... for value ' + param1)
}

Hope this help you

sasikals26
  • 835
  • 2
  • 18
  • 41