This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I wanted to create variable name based on value sent to function in javascript.
like following, when i call function variable : variable("zebra"); this should return variable name as zebra1
function create variable(i){
return i+"1";
}
var variable("zebra")="abc";//this line should create variable zebra1 and initialise as abc
Try:
window['zebra'] = 'abc';
The window object holds all the global variables, assuming this is a request for global variables.
To specifically answer your question, you could put return window[i + '1'] = 'abc'; in your variable naming function.
Alternatively, you could create a global (or local) object named variables to hold all your variables:
function whoknows() {
var variables = {};
variables['zebra'] = 'abc';
}
Read more on working with objects at mozilla.org
You can create global variable with
window["zebra"] = "abc";
and use later ether with the same indexer syntax or directly - zebra.
Related
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
This is what I think is best for making an object in js
function obj(){
var x = "hi";
this.getX(){return x;}
}
var y = new obj()
console.log(y.x); //this returns undefined
But from what I have seen, using this.variable is used more often in object creation.
I am thinking in java where things should be "private" in a class (note I have read about closures), does that apply in js?
What is considered the best way of object creation?
It sounds like you're trying to apply Java concepts to JavaScript, but things works completely differently in JS. You should check out the MDN article on JavaScript closures.
var variables exist in the closure. They are accessible from any function declared in the same scope.
var me = 'hello';
function someFunction() {
console.log(me);
}
someFunction(); //prints 'hello' to console
this variables will be directly accessible, even outside the scope, in the resulting object.
function someFunction() {
this.me = 'hello';
}
var instance = new someFunction();
console.log(instance.me); //prints 'hello' to console
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I am aware you can access properties of objects in javascript using variables like this Object['property'] but I want to access the Object using a variable too.
// The object and his function
var Foo = {};
Foo.bar = function() { console.log("I am a useful function dood"); }
// The accessors
var obj = 'Foo';
var method = 'bar';
But when I try it in the way I think is right, I get the following. As all links in the world seem to be about accessing object properties with variables I don't seem to be able to sift one out that accesses objects like this too.
typeof obj // "object"
typeof Foo[method] // "function"
typeof [obj].foo // undefined
typeof [obj][method] // undefined <-- This is what I'm trying to use
Am I able to access the object like this?
EDIT
According to answers, if the object is globally scoped I could use window[obj][method] but the above is contained in an immediately invoked function like this:
(function(){
// The stuff from above...
})();
Will this work I will have to put this into an identifiable var that is globally scoped?
PS I will never ever use eval();
If your Foo object is at the global object then :
window[obj][method]() //I am a useful function dood`
Else , you will need to supply more info.
This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
Let say I got a string with the name of the variable I want to create.
If I want to create it with a global scope, I'd use window["myString"] = 100; or global["myString"] = 100; on Nodejs.
Is there a way to do the same but create the variable with local scope? (Ex: Inside a function)
EDIT: Note: The goal is to access the variable with its name directly. Ex: myString
I already know that I could easily create a object that would have the value as an attribute. Ex: obj.myString. But this is not what I'm looking for.
The local scope is not exposed as the global object is.
However...
function someFunction() {
var locals = {};
locals['someLocalVar'] = 'local var';
}
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 8 years ago.
Problem with example:
Variable name: var product5519 = 10;
I can get this name in the form of a String i.e
var str = "product5519"
Is there any way to convert it into variable name so that i can use the value assigned to
product5519
I know one way to solve this problem i.e using eval(str)
If there is any another way to solve it please tell?
Once you are certain creating a global variable was the Right Thing to do, you can add your variable to the window object, like so:
window[str] = 42;
This works because variable lookups end up trying the window object if the variable was not defined in an inner scope.
It's a bit hacky but if you wanted to make a global variable you could do:
var str = "product5519";
window[str] = value;
You could then access the variable like:
window[str];
window.str;
str; // Assuming that there is no local variable already named "str"
you could do something like:
window['product5519'] = 'value'
it may be better to have an array of products, depending on the situation ofc.
You can use an associative array:
var variables = [];
variables['product5519'] = 10;
console.log(variables['product5519']);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
jquery dynamic id
(4 answers)
Closed 8 years ago.
i need to access
objectName.myID
but the "myID" part is dynamically generated..
how do i do this?
i tried
this['objectName.'+ variable]
i'd hate to use eval...
ps
this happens in a function (local scope) by the way..
You can access Object properties in two ways:
o.propertyname
//or
o.["propertyname"]
When using the bracket notation you have to put the propertyname in quotes or else it will be interpreted as a variable name (which in your case is exactly what you want). So in your case where you have stored the name of the property as a string, the way to go would be:
var variable = "propertyname";
o[variable];
/* /\ variable is replace with it's string representation "propertyname" */
You can even call methods this way:
var o = {};
var functionname = 'toString';
o[functionname]();
You can mix both notations, your example would look like:
var obj = 'objectName';
var prop = 'myID';
this[obj][prop]
// or this is possible too:
this.objectName[prop]
Assuming propertyName is the name of a variable holding the name of the property, for example 'myId', then you can use.
objectName[propertyName]
More details in the MDN : Working with objects