simple question: "why does Eval() not work?"!

Asked

Viewed 187 times

0

I have a simple code to run, I never used Val, but I know its syntax:

var package = "edos.desktop";
eval(package + ".main()");

But it’s not working in my browser... I tried to use some alternatives like Function(package + ".main()"), setTimeout(package + ".main()");,setInterval(package + ".main()");, but amazingly, only the setInterval() worked, however, he kept repeating the code whenever he could...

my script with the main code:

var edos = {};
edos.desktop = {
    main: function() {
        document.writeln("este é um app!");
    },
}

and I need a solution, because eval() does not work, or some alternative to bypass the Eval...

DETAILS OF THE TESTED DEVICE:

Disposiivo: Moto G6 Play
Android: 8.0
Chrome Canary: 74
  • From what I understand, what you’re doing there is calling the function c() inside an object a.b. Better put the full code in the question and explain better what you intend to do.

  • Okay, but that’s exactly what I wanted to show you...

1 answer

2

Have you ever considered that the mistake might be something other than Eval? "Eval doesn’t work" is unlikely, there’s no way a bug like this could happen in a browser like Chrome, even on Canary. Test

const body = "document.body";
const firstChild = eval(body + '.firstElementChild');
console.log(firstChild);

It works, doesn’t it work? So the problem isn’t in Eval.

setInterval() worked, however, it kept repeating the code always that can

When you pass a string to setInterval, it runs the Eval command behind the curtains, another proof that there is nothing wrong with Eval. The only difference in this case is that setInterval will wait a little before running Eval, rather than running it immediately.

Your problem is not Eval, review your code. If on the other hand you insist on using something setInterval to solve the problem, use setTimeout, which has the same functionality as setInterval, but is only executed once.

  • That code you said above returned { }, that’s normal?

  • It returns the first element of body, if you run the code on an empty page, the return will be empty.

  • the first element is a <pre id="console">code <span>is</span></pre>, and yet you returned { }

  • should be because I do not use ES6, but rather Javascript...

  • 1

    Eduapps, please, Javascript is the implementation of the rules defined in ES6. I can’t tell you what the error of a code you didn’t post, but it’s no problem in the Eval command.

  • Dude, thanks, the problem was that I had picked up the file with the string "edos.desktop" through an Httprequest, and Eval was called before Httprequest was finished...

Show 1 more comment

Browser other questions tagged

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