IceCode
IceCode

Reputation: 1751

passing a function as a parameter?

Here is my event and as you can see I want to send a function with it as a parameter.

onclick="deleteItems('image', 'size', function(){GetImageSize();})"

The delete function is in a js file. In my js file I want to call my GetImageSize() function.

var deleteItems = function(module, controller, callback) {
   callback(); // ??????????    
}

I want to do this so I can rebuild my table when my ajax call has finished.

Hope you guys can help me

Regards Örvar

Upvotes: 2

Views: 201

Answers (2)

Daniel Vassallo
Daniel Vassallo

Reputation: 344511

JavaScript functions are first class objects. You could store functions in variables and pass them around as arguments to functions. Every function is actually a Function object.

Therefore, if you have a GetImageSize() function, you can simply pass it to the deleteItems() function just like any other variable:

deleteItems('image', 'size', GetImageSize);

Upvotes: 3

Nathan Hughes
Nathan Hughes

Reputation: 96444

You can refer to the function, without calling it--just don't use the ending parens. The parens mean you're calling the function. In the case of passing the function into another function as a callback you want to leave the parens off. (And you don't need the anonymous function declaration around it.) It should read:

onclick="deleteItems('image', 'size', GetImageSize);"

Upvotes: 3

Related Questions