最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript callback function with parameter? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jul 25, 2012 at 7:05 Royi Namir asked Jul 25, 2012 at 6:54 Royi NamirRoyi Namir 149k144 gold badges493 silver badges831 bronze badges 5
  • may be duplicated stackoverflow./questions/1997531/… – mask8 Commented Jul 25, 2012 at 7: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 Commented Jul 25, 2012 at 7:02
  • how about this one? stackoverflow./questions/3458553/… there are too much of this questions anyway – mask8 Commented Jul 25, 2012 at 7:07
  • @mask8 Ok , Agree. ( didnt find it before though) ...:) – Royi Namir Commented Jul 25, 2012 at 7:07
  • @RoyiNamir when that first question was written, ES5 had been approved less than a month prior. 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 Commented Jul 25, 2012 at 7:10
Add a ment  | 

4 Answers 4

Reset to default 4

The mon 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'));

One of the solutions is to use .bind:

doWork('a', 'b', myCb.bind(null, 'xxx'));

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

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

发布评论

评论列表(0)

  1. 暂无评论