variable binding inside an object

前端 未结 2 1045
借酒劲吻你
借酒劲吻你 2021-01-29 00:32

in this code below:

var a = 1;
var boat = { a:2, 
             innerBoat: {a:3, 
                        print(){console.log(a);},
                        },
            


        
2条回答
  •  醉酒成梦
    2021-01-29 00:57

    i don't understand why console.log(a); in print method returns the value 1.

    It logs the value of the variable a.

    The properties of the various objects around it which also have the name a are not variables. They can only be referenced as properties of an object.

    const a = 1;
    const object = {
        a: 2
    };
    
    console.log(a);
    console.log(object.a)


    See also How does the “this” keyword work? for how to access the property of an object that the method you are calling is attached to.


    Also, does the curly braces {} of the object referenced by boat creates a new private execution context in the stack at runtime ?

    No.

提交回复
热议问题