How to pass parameter to callback in javascript [duplicate] - javascript

This question already has answers here:
JavaScript: Passing parameters to a callback function
(16 answers)
Closed 6 years ago.
I would like to pass some parameter in call back like the following example. Is it a possible? Or I need to do it in another way?
function testCallback(callback, destination){
// how can I pass the destination to callback
callLib(callback)
}
function myCallback(destination){
//do something
}
callLib is an external library that accept. I guess the interface should be like this
function callLib(callback){
callback("something")
}
this one I think is not duplicate one. Sorry for my bad english. I have already change the question. see if you understand what the problem is

I played with callbacks all day to learn how to do this. Here is some example code: https://stackoverflow.com/questions/37554672/jqueryui-dialog-doesnt-stop-code-to-wait-for-user-input/37556573#37556573

Related

Do I have to address JavaScript function variables in order? How to do it? [duplicate]

This question already has answers here:
Skip arguments in a JavaScript function
(9 answers)
Closed 9 months ago.
How do I send one or more variables in a JavaScript function without sending null values to unused arguments?
I have a function:
function setProjectParameters(trigger, pp_ts, pp_t, pp_bs, pp_dg, pp_ll){alert("the rest of the code is here doing something");}
Sometimes I want to call this function and only send a value to pp_bs, now I do it like this:
setProjectParameters(null,null,null,"8");
Can I do it something like this?: (It doesn't work so I guess not...)
setProjectParameters(pp_ts="44");
What is the proper way to do it?
The easiest I think is to have the parameters in the function as an object and have defaults for them if you need to. Then you can target a specific parameter.
function setProjectParameters({trigger = 'if you need defaults add them like this', pp_ts, pp_t, pp_bs, pp_dg, pp_ll}){alert("the rest of the code is here doing something");}
Then use the function like this
setProjectParameters({pp_ts="44"});

Functions that call eachother [duplicate]

This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 2 years ago.
I am looking for someone to explain me something since I can't find a clear answer. My question is about functions that call themselves. I saw that it is possible to build a list of functions that are 'chained' together and for example the first function calls the second one then the second one calls another one. My confusion is : Lets say that you have a second function that has a variable let a = 12; if i call that function on the first function, will i have access to that variable or whatever that second function might have inside? How can i pass info to another function? can a function can be dependent on another function in order to complete a task? Thanks in advance guys.
Edit to make it more clear what I mean:
function first(){
second();
// will i have access to whatever there is inside function second since i am calling it here ? or it doesn't work that way?
}
function second(){
let a = 12;
third();
}
function third(){
fourth()....
}

i cant pass a parameter to a function using javascript [duplicate]

This question already has answers here:
How can I pass a parameter to a setTimeout() callback?
(29 answers)
Closed 2 years ago.
i am trying to add a set time out function like this and it does not work
setTimeout(myfunction(parameter),200)
meanwhile the code below work but it does not get the job done cause i cant pass a parameter.can anyone explain why ?
setTimeout(myfunction,200)
Try
setTimeout(function() {
myfunction(parameter);
}, 200)
You can also use
setTimeout(myfunction.bind(null, parameter), 200);

Call function programmatically/"by string" in coffeescript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dynamic object property name
I have a function that I need to call based on a user inputted data.
So for example, I have:
models.cat
models.dog
Now, I want to be able to call models.[my_str] where my_str = "snake". So the computer would think it is trying to execute models.snake. Is there a way to do this in javascript or better yet coffeescript?
You should be able to call it like so:
models[my_str]();
This should work in both Javascript and Coffeescript.

JavaScript - Get calling object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.

Categories