Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
How to use this keyword inside eval in javascript?

Having this:

const person = {
  name: "James Smith",
  hello: function (x) {
    console.log(this.name + " says hello " + x);
  }
}

function newContext() {
  this.name = "Someone else";
  const code = "(function(){person.hello}).bind(this);"
  eval(code);
}

newContext();

There is not output. But don't know why

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You aren't calling the function here:

(function(){person.hello}).bind(this);

is

(function(){
  person.hello
}).bind(this);

where person.hello is just an unused expression. You need person.hello (not wrapped in a function) to be on the left-hand side, and you also need to actually call it, with () afterwards - or by using .call. You also might want to pass an argument (the x).

const person = {
  name: "James Smith",
  hello: function (x) {
    console.log(this.name + " says hello " + x);
  }
}

function newContext() {
  this.name = "Someone else";
  const code = "person.hello.call(this, 'xx');"
  eval(code);
}

newContext();

const person = {
  name: "James Smith",
  hello: function (x) {
    console.log(this.name + " says hello " + x);
  }
}

function newContext() {
  this.name = "Someone else";
  const code = "person.hello.bind(this)('xx');"
  eval(code);
}

newContext();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda