Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

49
Vistas
how javascript handles variables in objects?

I have a question about declaring a variable in an object. Assuming that global or window is also an object, why is it not possible to declare a variable using let in an object that is a child of the window object? I do not understand that. Thanks for the answer and sorry for the English but I hope you understand the question.

this doesnt worked

let a = 'global';
console.log(a);
const outsideObj = {

    let b = 'outside var',
    logIt() {
        console.log(this);
        console.log(a);
        console.log(this.b)
    }
};

outsideObj.logIt();

this worked

console.log(this);

let a = 'global';
console.log(a);

const outsideObj = {

    b: 'outside var',
    logIt() {
        console.log(this);
        console.log(a);
        console.log(this.b)
    }
};

outsideObj.logIt();

i dont get difference between windows object and regular object why is it possible in parent and not in child?

6 months ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

An object has properties and can be created by an object literal with property definitions.

A scope has variables and can be created by a block statement with variable declarations.

Do not mix these two concepts, especially not syntactically. That global var and function declarations implicitly create properties on the global object is a special case, and also only works by the global object being provided by the runtime - there is no object literal for it.

6 months ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.