To add private variable to this Javascript literal object

zyip發表於2015-03-16

 

You can use number as function/variable name, the numberic name can't be accessed from parent scope, but can be accessed by 'this' in private scope.

 

var o=
    {
      attr1:'value of attr1',
       1:'private attr ,the index is 1',
       301:function(){
         console.log('private function , name is 2');
       },
       fun1:function(){
        console.log(this[1]);
         this[301].call(this);
      }
    };

o.fun1();

 

相關文章