0
I have a string, caught by a function, called package
, and its value is equivalent to "edos.desktop"
.
function callMain(package) {
//queria fazer tipo assim, é apenas uma suposição:
package.main();
}
As you can see above, I want to call a function that calls main()
within each typed package.
Here are two examples:
For each:
callMain("edos.desktop");
The function would make:
edos.package.main();
And I wanted it to be independent of package. For example, I want to call thousands of Packages, one different from the other, but all with dots ("."
).
And then not to type a thousand times:
package.main();
I created this function, which would convert such string to variable and call the function main()
.
In short, I want to do:
//(dentro da função callMain())
"edos.package".main(); //quero que isso vire isso:
edos.package.main();
I think I’m going to go unanswered, plus a negative vote that I don’t even know why...
– EduApps
Your question is confusing. I don’t know if it’s clear to you what it is
string
, object and function yet. But, to give you a light, take a look in that reply and also in the concept of val. Maybe it’ll shed some light– Caio Felipe Pereira
1- Please do not capitalize the whole title and do not put tags that are unrelated to the question, this is just noise and does not cause answers to appear faster. 2- It is Sunday and the site becomes emptier even, have patience that during the week has more active users. 3- Maybe what you need is the
eval
- enjoy and read also that and that to better understand the implications of usingeval
– hkotsubo
I had explained the Val in a different way, I thought the function had to be outside the string.. as I used
eval("objeto.objeto").função();
instead ofeval("objeto.objeto.função")()
...– EduApps
Anyway, I’m gonna try...
– EduApps
You can pass any valid code within the Eval, including function calls: https://jsfiddle.net/tbh32da5/
– hkotsubo
Dude, I don’t know if it’s because I use the original javascript, but when I put this
eval(package + ".main()");
it did not work, even after this command includes an Alert saying "it worked", but neither Alert ran...– EduApps
Perhaps it is the case of ask another question, putting the code you are using and describing in more detail the problem (remember to make a [mcve] so that everyone can reproduce the problem you’re having).
– hkotsubo
But the file I’m having the problem has more than 200 lines...
– EduApps
@hkotsubo , I found the problem, is the Val, I used
setInterval(package + ".main()", 50);
and it worked!! But the problem now is another, the set interval keeps repeating the functionmain()
all the time...– EduApps
Then, the link I passed in the previous comment (the [mcve]) gives several tips to reduce the code and post here. Gets the tip for the next :-)
– hkotsubo
Okay, thanks, but I still don’t understand why Eval doesn’t work...
– EduApps
OK I’ll try again in another post...
– EduApps