Object literal is considered as a static object.
So, object literal should contain only static variable, but in the following piece of code
var obj = {
a : "hello",
foo : function(){
console.log(this.a);
console.log(obj.a);
}
};
I can access a, in a static way obj.a and in a non-static way this.a.
Is a a static variable?
I think you are confusing a bunch of different things.
You’ve created an object named
objthat has a field nameda. That field can be accessed asobj.a(orobj['a']if you prefer). If you arrange for some other variable to refer toobj, then it’s also available via that variable.One way to arrange for some other variable to point to
objis to take a field ofobjwhich is defined as a function/closure, and invoke it using “method” syntax, as inobj.foo(). That means that inside the body offoo, for the duration of that invocation, the special variablethiswill refer toobj. Therefore code within that function can accessobj.aviathis.a.None of this has anything to do with static vs dynamic scope, or singletons, or “class” vs “instance” members, or any of that. It’s just an object.