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

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.

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"});

How to pass parameter to callback in javascript [duplicate]

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

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

javascript add one object to be a member of another [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I have a Javascript object that I use for global variables, username, userid, etc.. I call it myGlobals. I have a need to load another object via jQuery (application preferences) that I will also need to access globally. For cleanliness, I want it to be a part of myGlobals.
I see posts on how to "merge" the two objects, or add the second one as an array, but that is not exactly what I want. I have this:
var appPref = JSON.parse(xhr.responseText); //this works fine
myGlobals.appPref = appPref; // this does not
I want to be able to do something like this:
alert(myGlobals.appPref.messages.hello);
but I am getting a "myGlobals.appPref: is undefined.
How do I do this? I am using jQuery if there is some magic in there that I could use.
myGlobals.appPref = appPref;
This is how you do it.
The problem is somewhere else. Try to put a console.log(appPref); before this line. Most likely it will show undefined.

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