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

100
Visualizações
Prevent Stringifcation of Objects Passed Into Eval

Consider this code:

function set(n,v) {  // This is a setter
    eval(n+"="+v);
}

let one = 1;
function add1(num) {
    return num + one;
}

{
    let two = 2;
    set("add1", function(num) {
        return num + two;
    });

}

// Add 1 now equals: 
//  function(num) {
//      return num + two;
//  }

// So if we call add 1:
add1();

// We get a "Uncaught ReferenceError: two is not defined"

This is because eval stringifies the function which removes its connection to variables it counts on

How do I stop this (keep the set behavior, but prevent the function from leaving behind variables)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

DON'T USE eval for variable variables. Use objects instead, as explained here

Now, to answer your question: as a direct eval can access your local variables, all you have to do is read them inside the eval string, instead of passing from the outside:

function set(n, v) {
    eval(n + "= v");
}
about 4 years ago · Juan Pablo Isaza Relatório
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