0

Ok guys, so I have this code:

var q=(function(){
    var u,q,x;
    x=616;
    q=q_construct();
    qp_construct();
    return q;
    function q_construct(){
        var q,pro;
        q={
            a:1
        };
        return q;
    };
    function qp_construct(){
        console.log('1 ⇒    ',q,x);
        Object.defineProperty(Object.prototype,'q',{get:function(){
            console.log('2 ⇒    ',q,x);
            var r={b:2};
            return r;
        }});
    };
})();

When I enter y={z:616}; y.q; into the console, I get:

1 ⇒  Object {a: 1} 616

y={z:616}; y.q;                 //  console entry point

2 ⇒  Object {a: 1} 616
Object {b: 2}

Yet if i change:

var r={b:2};
return r;

to

var q={b:2};
return q;

the result changes from:

2 ⇒  Object {a: 1} 616

to

2 ⇒  undefined 616

I can not find out why, when setting this variable name to q, the q from the inherent scope is redefined as undefined. Something is niggling in the back of my head, saying I read something on "hoisting" that might help?

LostInCyberSpace
  • 413
  • 4
  • 19

0 Answers0