Create a variable named after a string parameter in JavaScript [duplicate] - javascript

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I know this piece of code is wrong, but I wonder if there is any way this would be possible.
var createVariableFromParameter = function(myName) {
var myName = [];
return myName;
}
Thank you.
EDIT: I found the solution here: Convert string to variable name in Javascript

I think it's not possible, and even if it could be done, why would you want to do it? There is no meaning in creating variables with random names...

I don't know why you should need it??
But I think true way is :
function create(myvar) { myvar = [];}
var a;
create(a);
function create() { return [];}
var b = new create();

Related

Unable to access variable within a function [duplicate]

This question already has answers here:
return a string inside a function
(2 answers)
Closed 1 year ago.
Here is the challenge I need help with:
Create a variable called myFriend using one of the variable declarations described above (var, let, or const). Set the variable you created to contain your friend's name. Inside the greetings function, return the string: "Greetings [your-friend's-name].
New to this, but I can't seem to find out where the error is. Probably easy for you guys but, I'll get there.enter image description here
var myFriend = "Sara"
function greetings() {
let greetings = "greetings, " + myFriend
}
console.log('results: ,'+greetings());
You forgot the return statement.
return greetings;

Calling javascript function using dot operator [duplicate]

This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
function getPercCalculated(x){
return (x*9.3)/100;
}
var x = 10;
var perx = getPercCalculated(x);
Instead of this I would like to call the getPercCalculated using dot operator like
var perx = x.getPercCalculated()
Can someone help me..!!
Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough

JavaScript - Call a function by a string [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 8 years ago.
This question is already existing in a different way, but my code is mostly different so I dont get the solution out of the other answers. That's a example Code:
http://jsfiddle.net/e52n28xs/
I want start the function like this:
var test1 = {
start: function() { }
};
var fn = 'test1';
fn.start();
I know the following works, but I need the string option.
test1.start();
Maybe that's impossible?
You could do it with eval() function
var test1 = {
start: function() { }
};
var fn = 'test1';
eval(fn).start()
DEMO
Depending on the value of this in the function you run this, either of these will work:
window['test1'].start();
Or
this['test1'].start();
Obviously, you can do the same with the function name itself, example:
test1['start']();
or
this['test1']['start']();
// same as: var test1 = this['test1']; test1['start']();
If you want the whole thing to be string, then:
eval('test1.start()');
That would also work if you are using var, where in the previous string versions you'd have to have a container object to query.

How insert variable in JS [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}

How to use a String variable as key in a JSON object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is a way that use var to create JSON object in key?
I would like to construct a JSON object in JavaScript by using the value of a (String) variable as the key. But what I got is the name of the variable as the key.
example.js:
function constructJson(jsonKey, jsonValue){
var jsonObj = { "key1":jsonValue, jsonKey:2};
return jsonObj;
}
The call
constructJson("key2",8);
returns a JSON -> {"key1":8,"jsonKey":2} but I would like to have {"key1":8,"key2":2}.
Does anyone know how to achieve this?
seems like a simple problem but I cannot find the solution.
function constructJson(jsonKey, jsonValue){
var jsonObj = {"key1": jsonValue};
jsonObj[jsonKey] = "2";
return jsonObj;
}

Categories

Resources