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

160
Vistas
Why global scope can not be accessed in this case?

I faced this issue recently. I could pass the problem, but I didn't get the concept behind it. If you try the snippet, you see what's going on. Can someone explain it?

function func1() {
  console.log('Func1 executed.');
};

const func2 = () => {
  console.log('Func2 executed.');
};

const test = () => {
  window['func1'](); //Working
  window['func2'](); //Not working
};
button {
  width: 200px;
  padding: 20px;
  font-size: large;
  cursor: pointer;
  border: 0;
  background: linear-gradient(to top right, gray, silver);
  color: white;
  border-radius: 7px;
  box-shadow: 0px 5px 7px silver;
  text-shadow: 0px 2px 2px rgba(0,0,0,0.5);
}
<button onclick='test()'>Execute</button>

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

0

The straightforward answer is:

Global variables initialised with let and const do not become properties of window.

See:

const

Global constants do not become properties of the window object, unlike var variables

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

and:

let

Just like const the let does not create properties of the window object when declared globally (in the top-most scope).

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

about 4 years ago · Juan Pablo Isaza Denunciar

0

The issue is that you are really calling a value/variable (const func2 = could end with a int for example.) You need to pass a function itself int tohe format window[Functioname]().

You can do this through 2nd degree function calling, see below.

Nice button by the way.

function func1() {
  console.log('Func1 executed.');
};
const func2 = () => {
  console.log('Func2 executed.');
};


function func3(){
  func2();
}
const test = () => {
  window['func1'](); //Working
  window['func3'](); //Working
  window['func2'](); //Not working
};
button {
  width: 200px;
  padding: 20px;
  font-size: large;
  cursor: pointer;
  border: 0;
  background: linear-gradient(to top right, gray, silver);
  color: white;
  border-radius: 7px;
  box-shadow: 0px 5px 7px silver;
  text-shadow: 0px 2px 2px rgba(0,0,0,0.5);
}
<button onclick='test()'>Execute</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm not very experienced with JS, but I believe is because the first one is actually a function that can be reused multiple time, where the second one is lambda function with the purpose to returning and storing a value (but not recompute) into the func2 var.

Correct me if I'm wrong.

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