Get the 'name' of a variable in Javascript [duplicate] - javascript

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Determine original name of variable after its passed to a function.
I would like to know if its possible to get the actual name of a variable.
For example:
var foo = 'bar';
function getName(myvar) {
//some code
return "foo"
};
So for getName(foo) will return "foo"
Is that possible ?
Thanks.

I don't think it is possible. When you call a function you pass an object, not a variable. The function doesn't care where the object came from.
You can go the other way though if you call your function as follows:
getName('foo')
Or pass both the value and the name:
getName(foo, 'foo')

Related

Turn a string into function in JS [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 2 years ago.
My problem is simple and I couldn't find the proper answer in this forum. My bad...
I want to do that :
const dataReceived = foo;
foo(state);
How can I do that?
I read it is better to avoid eval, and I couldn't get success with new Function.
Thanks for your help!
EDIT
Thanks for your answers.
I work with React.
In my reducer, I have a create_item case.
I can reach action.category, that can be the word 'currency' or 'country'.
What I want to do is to launch either the method createCurrency or createCountry according what is inside action.category.
That's why I tried to join 'create' and 'action.category' to create a dynamic function name.
But it seems to be a poor idea...
The simplest approach is to create an object which contains an entry where:
the key is a string
the value is a function.
Example:
const myObject = {
myFunction: () => { [... DO SOMETHING...] }
}
Subsequently you will be able to invoke the function, using:
myObject.myFunction();
The above becomes more powerful when you use brackets notation.
Example:
const myString = 'myFunction';
myObject[myString]();

Get function's this binding [duplicate]

This question already has an answer here:
How to inspect a JavaScript Bound Function
(1 answer)
Closed 2 years ago.
Given a function,
function main() {
// some logic
}
Lets assume the function main is bind with const obj = { name: "John Doe" }
like const fn = main.bind(obj);
Now the question is, Is there a way to get the fn function binding?
Note: i know binding can be accessed using the this keyword inside the main function but is there any way to access this value outside the context. is there any magic (hypothetical) method like fn.getContext().
Thank you for your time.
No there is not. While the new function object has an internal [[BoundThis]] slot, that slot is not accessible via a user-facing API.

How to create a function that can be called on an object in javascript [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
So, for learning purposes I'd like to recreate the "charAt()" existing method in Javascript which tells you the position of a character in a given string. I'll call the method "CharAtX"
To do so, I've created a function with 2 parameters : The first one is the word, the second is the position, here is the code I have :
function charAtX(word,pos) {
word_split = word.split("");
return(word_split[pos])
}
console.log(charAtX("Truck",2))
So, it obviously works, if i call charAtX("truck",2), i will have "u" returned.
But my question is the following :
The original charAt can be called like such
my_word.charAt(3)
Mine can't though. Why is that and how could I change my function into a method so that I can?
You have to call charAtx function in the context of String object.So, When you call string.charAtx, this object refers to the string. You have to learn prototype and this object in jvascript.
String.prototype.charAtX = function(pos) {
word_split = this.split("");
return(word_split[pos])
}

How do you get the string of a variable name in javascript? [duplicate]

This question already has answers here:
Variable name as a string in Javascript
(20 answers)
Closed 9 years ago.
I have a variable as such:
var foo = ["cool", "cool"];
How do I get the variable name, foo, as a string? In other words, given foo, how do I get "foo"? I want to use it in the following context where "foo" is used in getElementById(...):
document.getElementById("foo").innerHTML = document.getElementById("foo").innerHTML + "";
Thanks!
You're looking at this the wrong way round - to answer this question directly
given foo, how do I get "foo"?
Well, the answer is... type "foo" in your code.
You know you want the string representation of the foo variable name when you write your code, so there's no scenario where the string value you want would be anything other than "foo", right?
It's true that you can reference a property foo, for example of the window object, using window["foo"] but that's the other way round too - you have to know it's foo you want, and deliberately use "foo" to get at it. So there's an example - you know what string to use, "foo", at compile time - it couldn't possibly be anything else.
If it's a global variable, you get it as global object property: window in browsers and global in NodeJS. Otherwise, you don't.

Is it possible to make User defined DOM changes in runtime? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript object, access variable property name?
I am messing around with JS, learning a stuff and I am wondering about something...
Say you have a function aFunc(), and you accept a string aFunc(val). The value is user defined and is then used to modify the CSS of an element.
For Example:
function aFunc(val){
document.getElementById('something').style.val = 'red';
}
Say the user entered borderColor, it would somehow refrence borderColor where val is. I do not know how or if this is possible.
EDIT:
Please no eval() :)
Just use this as a base: JSBIN- Demo on a Div
var type = prompt("style");
var value = prompt("value");
document.body.style[type] = value;
Every object in JavaScript has a method called hasOwnProperty which takes a string value and will return a Boolean value.
var myObj = {
name: "Josh"
};
myObj.hasOwnProperty("name") === true; //This is true
You can use that to test for the presence of a particular property and then use the method stated in Akhil Sekharan's answer to access that property.

Categories