This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
This code doesn't work:
var my = {
testFunction: function(text) {
alert(text);
}
};
var functionName = "my.testFunction";
var f = window[functionName];
f('yeha');
Any idea why?
Update:
I don't know in advance the functionName. It might be 'my.testFunction' or 'my.test.another.function' etc.
I'm writing a validation handler and all my js will know is a string representing a function that could be a function inside an object.
This should work.
var my = {
testFunction: function(text) {
alert(text);
}
};
// the string can't be evaluated as nested object hierarchy.
// split it to address the several nodes and properties
var functionName = "my.testFunction".split('.');
var f = window[functionName[0]][functionName[1]];
f('yeha');
Related
This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Using Variable for Property Name of Object - Javascript [duplicate]
(2 answers)
Closed 4 years ago.
I have an array that contains variables that are used to control a template.
divisionsListToHide: ['showActivitiesList',
'showAssignActionplanDiv',
'showPropertiesListDiv',
'showAddActivityDiv',
'showCurrentActivity',
'editCurrentActivity'
],
I want to call a method that will set all variables contained in the divisionsListToHide array to false, i.e.
this.showAssignActionplanDiv = false;
this.showPropertiesListDiv= false;
I am trying to create a method (function) to accomplish this:
hideDivisions: function()
{
for(var i = 0;i<this.divisionsListToHide.length;i++)
{
var xx = 'this.' + this.divisionsListToHide[i]; // e.g. this.showActivitiesList
console.log(xx);
eval(xx) = false;
}
},
I was hoping to accomplish this through eval(), however, it displays "Uncaught ReferenceError: Invalid left-hand side in assignment". Any suggestion, how to accomplish this?
this is an object, and you can access its properties in two ways:
this.propertyName and this["propertyName"]. These two ways are the same, except the second one can be used with a variable just like in your case:
var prop = "propertyName";
this[prop] = false; // is equal to this["propertyName] = false, which is equal to this.propertyName = false
So try this:
hideDivisions: function()
{
for (var i = 0; I <this.divisionsListToHide.length; i++)
{
this[this.divisionsListToHide[i]] = false;
}
}
This question already has answers here:
How to get function parameter names/values dynamically?
(34 answers)
Closed 6 years ago.
I'm trying to grab an array of string represented parameters from a function and I'm unsure how to proceed. Basically given the function below
function MyFunc(param1, param2, param3){
//do all the things
}
What would the function "getParams" look like to do the following
getParams(MyFunc) // ["param1","param2","param3"]
This is a bit messy, but you can do this by converting your function to a string and then splitting it until you get just the parameters:
var getFuncParams = function(MyFunc) {
var str = MyFunc.toString()
var strParams = str.substr((str.indexOf('(')+1), (str.indexOf(')') - str.indexOf('(')) - 1)
var params = strParams.split(",")
return params;
}
This question already has answers here:
Assigning the value of another key in javascript [duplicate]
(2 answers)
Closed 7 years ago.
According to me this is only being used if you need to call constructor like below
var dogObj = function(){
this.bark = "woof";
}
var dog1 = new dogObj();
console.log(dog1.bark);
Is there any cases where I use this on object literal?
var bark = {
this.bark = "dog"
}
Is above code valid? If yes,how do I call it?
you can use
var bark = {
bark : "dog"
}
and call it like
console.log(bark.bark)
this.bark = "woof";
Is not valid.
You have try something like below
var dog = {
bark : "dog"
}
And then you can access like console.log(dog.bark)
Once you formed any json string you can validate the sting using http://jsonlint.com/ just place your json string and the click on validate.
This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I want to pass a string for an array's name to a function, and the function create that array, e.g:
make_array('array_name', data);
function make_array(array_name, data){
array_name = [];
// do stuff
array_name.push(//stuff);
}
I don't want to have to create the array first manually
You can do .
window[array_name] = [];
You can use eval() to do it.
eval("var " + array_name + " = []");
If you just want the function to return an array, there is no need to create it beforehand. You can just do this:
function make_array(data){
var array_name = [];
// do stuff
array_name.push(//stuff);
return array_name;
}
var my_new_array = make_array(data);
This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Using a variable value to call an array element
(7 answers)
Closed 8 years ago.
Hard to explain.. I basicly want to do the following:
var doWhat = "speak";
var speak = {
hello: function() { alert: "Hello!"; }
};
// Won't work
doWhat.hello();
It's a bad example, but you should be able to get what I mean.
Is it possible somehow ?
You can use eval(doWhat).hello();. That way the contents of doWhat will be evaluated to the object reference.
You can do something like
var doWhat = {}, str = "speak";
doWhat[str] = {
hello : function() {}
};
doWhat[str].hello();
jsName = 'PageIndexController';
//ST1
eval("if( typeof jsName === 'undefined')alert(111);");
//ST1
eval("if( typeof " + jsName + " === 'undefined')alert(222);");
//ST1 not work
//ST2 work and the output: 222;
//there are two different way using eval, we will get 2 different outcome.