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;
}
Related
This question already has answers here:
Get JavaScript function-object from its name as a string?
(9 answers)
Closed 2 years ago.
That's about it. I have a string = "sum" and a function named sum(a,b), how can I substitute my string to call that function;
string = "sum"
function sum(a,b)
So basically to call that function, I would want to execute it like
string(a, b)
I suggest you use dictionary, where you define all existing functions:
const funcs = {
sum: function(a, b) {
},
someStuff: function(r, f) {
}
};
//call it:
funcs["sum"](a, b);
You can simply access your function via indexing with the strings
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');
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
function s(e) {
return e.toString();
}
var a = s(3); // works
var b = 3.toString(); // error
For example, set var a to the return value of s() that returns the first argument.toString(), but you can't set var b to 3.toString()
Javascript is expecting number(s) after the decimal, you can still do this, but you need to put your number in parenthesis:
(3).toString() = "3"
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);