Convert string to object in Javascript without Apis

Asked

Viewed 48 times

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...

  • 1

    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

  • 1

    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 using eval

  • 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 of eval("objeto.objeto.função")()...

  • Anyway, I’m gonna try...

  • You can pass any valid code within the Eval, including function calls: https://jsfiddle.net/tbh32da5/

  • 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...

  • 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).

  • But the file I’m having the problem has more than 200 lines...

  • @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 function main() all the time...

  • 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 :-)

  • Okay, thanks, but I still don’t understand why Eval doesn’t work...

  • OK I’ll try again in another post...

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.