Javascript: Get Argument Value and Name of Passed Variable

Determine original name of variable after its passed to a function

You're right, this is very much impossible in any sane way, since only the value gets passed into the function.

javascript: get names of actual arguments passed in a function

I need that because the names also contain some information which I need in my function logic

if i call func(a,b,c,d) then there is one flow if logic in func and if I call it with func(l,m,n,d) then the logic flow in func algorithm changes and accordingly the output will change too

There is something profoundly wrong with your understanding of JS, or your conception of your problem, or more likely both.

Functions cannot know, and do not know, and must not know, and have no reason to need to know, what variables, values, or expressions were used in calling them.

I have no idea if this will solve your problem, whatever it is, but consider doing the following instead:

func({a,b,c,d}); 

The {a,b,c,d} format creates an object with keys of a etc. whose values are the variable a. This is ES6 syntax. In ES5:

func({a: a, b: b, c: c, d: d}); 

Then, in your function, you can look at the keys in the object passed in as a parameter.

Javascript - Pass function argument into variable name

this is bad coding, but here is your answser:

var  redResource = 0,  greenResource = 0,  blueResource = 0;
var player_one = { addResource: function(num,color){ window[color+'Resource'] += num; document.getElementById(color).innerHTML = window[color+'Resource']; }}
#red { color:red }#green { color:green }#blue { color:blue }
<div class="contain">  <h3 id="red">0</h3>  <h3 id="green">0</h3>  <h3 id="blue">0</h3>  <button onClick="player_one.addResource(4,'green')">Add Green</button>  <button onClick="player_one.addResource(3,'red')">Add Red</button>  <button onClick="player_one.addResource(7,'blue')">Add Blue</button></div>

Use a parameter passed to a function as a variable name

Try this:

window[dataSource]

That will get the value of the variable in the string you have. For example:

dataSource = "something"
something = 5
alert(window[dataSource]) // alerts 5

Pass variables by reference in JavaScript

There is no "pass by reference" available in JavaScript. You can pass an object (which is to say, you can pass-by-value a reference to an object) and then have a function modify the object contents:

function alterObject(obj) {
obj.foo = "goodbye";
}

var myObj = { foo: "hello world" };

alterObject(myObj);

alert(myObj.foo); // "goodbye" instead of "hello world"

You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.

var arr = [1, 2, 3];

for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i] + 1;
}

It's important to note that "pass-by-reference" is a very specific term. It does not mean simply that it's possible to pass a reference to a modifiable object. Instead, it means that it's possible to pass a simple variable in such a way as to allow a function to modify that value in the calling context. So:

 function swap(a, b) {
var tmp = a;
a = b;
b = tmp; //assign tmp to b
}

var x = 1, y = 2;
swap(x, y);

alert("x is " + x + ", y is " + y); // "x is 1, y is 2"

In a language like C++, it's possible to do that because that language does (sort-of) have pass-by-reference.

edit — this recently (March 2015) blew up on Reddit again over a blog post similar to mine mentioned below, though in this case about Java. It occurred to me while reading the back-and-forth in the Reddit comments that a big part of the confusion stems from the unfortunate collision involving the word "reference". The terminology "pass by reference" and "pass by value" predates the concept of having "objects" to work with in programming languages. It's really not about objects at all; it's about function parameters, and specifically how function parameters are "connected" (or not) to the calling environment. In particular, note that in a true pass-by-reference language — one that does involve objects — one would still have the ability to modify object contents, and it would look pretty much exactly like it does in JavaScript. However, one would also be able to modify the object reference in the calling environment, and that's the key thing that you can't do in JavaScript. A pass-by-reference language would pass not the reference itself, but a reference to the reference.

edit — here is a blog post on the topic. (Note the comment to that post that explains that C++ doesn't really have pass-by-reference. That is true. What C++ does have, however, is the ability to create references to plain variables, either explicitly at the point of function invocation to create a pointer, or implicitly when calling functions whose argument type signature calls for that to be done. Those are the key things JavaScript doesn't support.)

Function argument as variable name

It's possible if you pass the variable name as a string.

buyprice1=200;
var buy = function(buyprice,buypriceAdd){
window[buyprice] = window[buyprice]+buypriceAdd;
}
buy('buyprice1',10);
alert(buyprice1);

http://jsfiddle.net/5v4chywv/

Javascript: Reference a variable name from the variable itself

The reason it doesn't work is because the variable foo is not accessable to the function varlog! foo is declared in someRandomFunction, and is never passed into varlog, so varlog has no idea what the variable foo is! You can solve this problem by passing the variable foo into the function(or using some sort of closure to make foo in the scope of varlog) along with its string representation, but otherwise, I think you are out of luck.

Hope this helps.



Related Topics



Leave a reply



Submit