Javascript scope chain

Posted by Geromey on Stack Overflow See other posts from Stack Overflow or by Geromey
Published on 2010-03-09T03:45:39Z Indexed on 2010/03/09 3:51 UTC
Read the original article Hit count: 409

Hi, I am trying to optimize my program. I think I understand the basics of closure. I am confused about the scope chain though.

I know that in general you want a low scope (to access variables quickly).

Say I have the following object:

var my_object = (function(){

        //private variables
        var a_private = 0;

    return{ //public
             //public variables
             a_public : 1,

             //public methods
             some_public : function(){
                  debugger;
                  alert(this.a_public);
                  alert(a_private);
             };
        };
})();

My understanding is that if I am in the some_public method I can access the private variables faster than the public ones. Is this correct?

My confusion comes with the scope level of this.

When the code is stopped at debugger, firebug shows the public variable inside the this keyword. The this word is not inside a scope level.

How fast is accessing this? Right now I am storing any this.properties as another local variable to avoid accessing it multiple times.

Thanks very much!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about variable-scope