Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
Converting `eval` statements to functions (implicit return)

I have developed a large number (>1000) strings that are intended to be fed into eval. I am trying to migrate away from using eval, as "eval is evil" and I'm generally trying to make things more secure. However, migrating from eval into a properly defined function is a little tricky, as eval() returns the value of the last expression evaluated. This means that a string that was previously evaled like this:

const stringToEvaluate = "x = 1; x + 1"

is difficult to migrate directly into a function, because the last expression in the statement does not explicitly return anything. With regards to the string above, I might use eval in this way:

const y = eval(stringToEvaluate)
console.log(y) // 2

However, I can't use:

const y = (function safeFunction() {
   x = 1;
   x
})()
console.log(y) // undefined

as safeFunction does not return anything.

Importantly, I am looking for a way to migrate these eval strings without needing to modify each individual string to include return statements in appropriate places. This is because the large number of scripts would make this quite inconvenient. Also, the scripts can be more complicated than this, but this is a nice, minimal example.

Does anyone happen to know of any way to migrate these scripts into functions without needing to manually edit each of them? Also, this is one my first times posting to StackOverflow, so I welcome any feedback about ambiguity in my question! Thanks!

Edit: Apologies for the ambiguity, another example of a "string" to be converted into a script is:

const stringToEvaluate = """
let x; 
if (
    $('.itemToFind').length > 0 &&
    $('.itemToFind').html().trim() != ''
  )
    $('.itemToFind del').html().trim();  <<< eval wraps either this
  else if ($('.price h4.offer-price').length > 0)
    $('.itemToFind').html().trim();      <<< or this
"""

(note that I have included the string across multiple lines to aid readability).

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda