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

147
Visualizações
Javascript new Function stack trace line numbers off by 2

Javascript stack trace line numbers for code within new Function are off by 2:

const fn = new Function(` // line 1
  try {                   // line 2
    blah                  // line 3
  } catch(e) {            // line 4
    return e.stack        // line 5
  }                       // line 6
`)

console.log(fn())

Running the above snippet produces:

anonymous@https://stacksnippets.net/js line 12 > Function:5:5

where it should say Function:3:5. Same thing when run in node:

$ cat stack-trace-line-nums.js 
const fn = new Function(` // line 1
  try {                   // line 2
    blah                  // line 3
  } catch(e) {            // line 4
    return e.stack        // line 5
  }                       // line 6
`)

console.log(fn())
$ node stack-trace-line-nums.js 
ReferenceError: blah is not defined
    at eval (eval at <anonymous> (/tmp/stack-trace-line-nums.js:1:12), <anonymous>:5:5)
    ...

The most obvious way to demonstrate is to put all the code on the same line:

const fn = new Function('try { blah } catch(e) { return e.stack }')

console.log(fn())

which produces:

anonymous@https://stacksnippets.net/js line 12 > Function:3:7

instead of Function:1:7.

Any idea what's the cause or if there's a workaround?

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

0

Log the function itself and you'll see what's going on:

const fn = new Function(` // line 1
  try {                   // line 2
    blah                  // line 3
  } catch(e) {            // line 4
    return e.stack        // line 5
  }                       // line 6
`)
console.log(fn);
console.log(fn())

It actually becomes:

 function anonymous(
) {
 // line 1
  try {                   // line 2

The first line is the function definition, the second line is the end of the parameter list and the start of the block, and only on line 3 does the actual body of the function passed into the constructor get inserted.

So, just add 2 to the line number every time.

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