JavaScript: How to pass an anonymous function as a function parameter? - javascript

I would like to write a function that accepts an anonymous function as a parameter. For example:
run('param1', function(){
alert('execute this');
});
function run(param1, callback) {
//now execute the callback parameter as a function
}
How can I achieve something like this?

callback() would invoke it.
If you need to supply a context, do callback.apply(this, arguments). When you use .apply be aware of the current execution context, basically know what this will refer to, or your code will not work as expected if you are feeding a literal that references this inside it's function body.

Related

How to run an anonymous function that was given as parameter

I know i can have a function as a parameter in javascript. I can also run that function that was given as a parameter. Here's an example:
function myfunction(param1, callbackfunction)
{
//do processing here
callbackfunction();
}
What if i wanna give an anonymous function, how will this run inside the outer function.
For example with a function like
setTimeout(function(){
})
How is this anonymous function directly run without a name?
You don't need a name to call a function. Names are only useful for use in a debugging tool (e.g. when examining a stack trace).
To call a function you need an expression that resolves as the function, which you can follow with ().
You're passing the function as the first argument to setTimeout, so it gets stored in the first parameter of that function. setTimeout's internals then call it.
You do the same with your code, only it is the second argument.
myfunction("some param", function () { /* ... */ }) ;
Maybe this makes it clearer?
function callFunction(param1, callbackfunction) {
console.log('callFunction', param1)
//do processing here
callbackfunction(param1);
}
function myCustomCallback(param) {
console.log('myCustomCallback', param)
}
setTimeout(callFunction.bind(this, 'one', myCustomCallback ), 1000)
setTimeout(callFunction.bind(this, 'two', function(p) {alert(p)} ), 2000)

How can I call a function with parameters without using an anonymous function?

I have the following Problem, I want to call a function b like I call the function a.
Here is the example:
$('#clickme').click(function () {
console.log("abc");
});
function a() {
console.log("a");
}
function b(b) {
console.log(b);
}
$('#clickmea').click(a);
// how to call this method without using a anonymous function?
$('#clickmeb').click(b("ab"));
Or is it not possible to call b("ab") without using a anonymous function?
JSBin exmaple
You can't do that without using either a anonymous function:
$('#clickmeb').click(function(){b("ab")});
Or a wrapper function:
function callback(){
b("ab")
}
$('#clickmeb').click(callback);
There's no need to return a anonymous function from the callback. That's just extra code that doesn't do anything useful.
In fact, it wouldn't carry over variables passed to the callback:
function callback(){
return function(){
b("ab") // There will be no arguments passed to this anonymous function, unless you explicitly add them.
}
}

Custom function not working?

I'm passing the click event into my function but doesn't seem to be working?
$('body').on("click", youclick(event));
function youclick(e){
console.log("testing");
e.stopPropagation();
}
Just pass the reference of that function itself. And you don't need to pass the event, you can receive that through global scope,
Try,
$('body').on("click", youclick);
function youclick(e){
console.log("testing");
e.stopPropagation();
}
As #Rajaprabhu suggested. When you are using a callback function(youclick(e)), the callback argument(youclick) is passed as the function name itself without arguments or even the opening or closing braces.
It means that a reference of the function is passed in the function call.
How CallBack Function works?
Because functions are first-class objects in JavaScript, we can treat
functions like objects, so we can pass functions around like variables
and return them in functions and use them in other functions. When we
pass a callback function as an argument to another function, we are
only passing the function definition. We are not executing the
function in the parameter. We aren’t passing the function with the
trailing pair of executing parenthesis () like we do when we are
executing a function.
And since the containing function has the callback function in its
parameter as a function definition, it can execute the callback
anytime. This allows us to execute the callback functions at any point
in the containing function.
It is important to note that the callback function is not executed
immediately. It is “called back” (hence the name) at some specified
point inside the containing function’s body.
$('body').on("click", youclick);
function youclick(e){
console.log("testing");
e.stopPropagation();
}

Is this a correct way of JavaScript/jQuery callback?

This is a simple question. Here is my code:
$(document).ready( function () {
func1( "foo", callback);
function callback(param){
alert(param+" is my name");
}
function func1(name, cb) {
cb(name); // alerts "foo is my name"
callback("bar"); // alerts "bar is my name"
}
});
I want to know:
Which one of the function calls inside func1 is the correct callback and why?
Or are they both correct?
Isn't callback("bar"); a normal function call?
Callbacks are meant to let a caller specify what a function should do at some defined point in that function's execution. The function being called shouldn't know the name of that callback function ahead of time. So they'll often be passed to a function as an argument and the function that's supposed to call the callback should just invoke that argument.
When you call callback("bar") in func1, you're totally missing the point of callbacks. You may be invoking the function that you happen to use as a callback, but the point of callbacks is that func1 isn't supposed to know about that. It's just supposed to call the function that's been passed in as an argument (cb). When I'm calling func1 I should be able to pass a completely different callback function and func1 should just call that function without knowing what its name is (it may not even have one!).
The "correct" way is cb(name).
callback("bar"); is directly invoking the callback function where as cb(name); calls the reference passed to the func1,
cb(name); seems to be the correct way here.
First one. Function calls another one which has been pased as a parameter.
It seems like most jquery methods follow this this form for callbacks:
$(SUBJECT).method(function() {
//do stuff
}, /*callback here*/ function(){
//do stuff
});
like for instance
$(foo).click(function() {
$(bar).fadeIn(300, function(){
//call back here
});
});
fiddle

how to call a function with passing parameter?

i want to pass the event to function in javascript suppose. i am fire function from two input. then how i can call the event with passing parameter.
can i do this using calling and without binding event in jQuery.
I'm not exactly sure what you're asking, but here's an example of passing a function with parameters as a callback:
setTimeout(function() {
return myFunction(false, 3);
}, 1000);
You wrap your function call in an anonymous function wrapper.
Example:
function DoCSSStuff(param1,param2)
{
$(param1).css('height', param2);
};
That way you could call the css function to manipulate height with passed parameters.

Categories

Resources