Passing variable in function in Javascript [duplicate] - javascript

This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 5 years ago.
When you pass variables in Javascript functions, what is the difference between '$name' and 'name'.
For example,
var myFunction = function(name){
name.a = "a";
};
var myFunction = function($name){
$name.b = "b";
};

The only difference in your example is the name, it has no special purpose.
However you might have seen the $name styled variables in jQuery examples in which it's normal to name selected elements ($('a')) as, for example, $links.

Related

Name of a variable as string, how to get value? [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
A javascript newbie question:
Let's say, there is a variable defined as follows:
var var1 = "Something"
On the other hand, I generate such strings (simplified):
var nmbr = "1"
var varname = "var" + nmbr
Now I have to get the value of a variable with such a name (varname as string).
document.write([???varname???])
should write 'Something'.
How to do this?
console.log(variable_name);
will print the variable in the console.

Why can't I do 1.toString() but I can do var a = 1; a.toString() [duplicate]

This question already has answers here:
Usage of toString in JavaScript [duplicate]
(3 answers)
Closed 6 years ago.
When I do
1.toSting()
I get an error, but
// javascript
var a = 1;
// or c#
int a = 1
a.toString()
works. Why is it that when a number gets assigned to a variable, it get some special functions?
The . is interpreted as you want a decimal/floating-point literal, not invoking a member.
You can do this in JavaScript
// Option 1
(1).toString();
// Option 2
1.0.toString();
// Option 3
1..toString();
In C#, it appears your only option is (1).ToString(), but the lexer might be smart enough not to need them.

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 ?

Calling a jQuery function using a variable name [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
I have the following code where MODE is a variable. Its value should be a method of jQuery's tinycolor liberary.
$cor = tinycolor($cor).mode(z).toString();
I'd like to call that method on that line so, for instance, when mode = 'lighten' $cor would be
$cor = tinycolor($cor).lighten(z).toString();
Is there a way of doing it this way?
Thanks!
Use bracket notation
$cor = tinycolor($cor)[mode](z).toString();

Using the value of a string variable as object parameter accessor [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I am attempting to dynamically access an object with a property value that is contained within a string. Example below:
var toolState = {
draw_point: false;
draw_line: false;
}
var dynamicText = "draw_point";
toolState.dynamicText = true; //here is the problem
I'm fairly new to JS. Sorry if this is a silly question.
Thanks
Use bracket notation instead of dot notation for variable names as properties.
toolState[dynamicText] = true;

Categories