JohnJohnGa
JohnJohnGa

Reputation: 15685

Object literal and static variable

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?

Upvotes: 2

Views: 805

Answers (4)

Dr.Sai
Dr.Sai

Reputation: 383

This is not so simple , check the

 let baba = {
    a: 12,
    foo: function () {
        baba.a = 13;
        this.a = 14;
    },
    roo: function () {
        alert("baba.a = " + baba.a + "\nthis.a = " + this.a);
    }
};
var theApp = {
    start_action: function () {

        let sola = Object.create(baba);
        sola.foo();
        sola.roo();

        let bola = Object.create(baba);
        bola.roo();            
    }
}
$(document).ready(theApp.start_action);

first call to foo after a roo() call we get : 13, 14 second call to foo 13, 13

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173562

The object literal is not a static class, it's an instance of Object; therefore obj.a can't be static either. Perhaps the confusion lies in the fact that the {} notation actually creates an object:

typeof {
    a : "hello",
    foo : function(){
        console.log(this.a);
        console.log(obj.a);
    }
};

"object"

It's equivalent to:

var obj = new Object();
obj.a = 'hello';
obj.foo = function() {}

Conclusion

Static properties only have a meaning in class-oriented languages, but the same concept of classes in JavaScript is arguably non-existent.

Upvotes: 3

Matt Zeunert
Matt Zeunert

Reputation: 16561

obj.a is "static", the way you refer to it. You would only use this in this way when using new obj() - so obj would have to be a function.

By default this refers to the parent object when used inside a function that is an object member. In the case of your code obj is the parent object.

An example of using prototypes and this:

var Obj = function(){this.a = "hi"};
Obj.prototype.foo = function(){
    console.log(this.a);
}
Obj.b = "sth";
var myObj = new Obj();
myObj.foo(); // "hi"
console.log(myObj.b); // undefined

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95252

I think you are confusing a bunch of different things.

You've created an object named obj that has a field named a. That field can be accessed as obj.a (or obj['a'] if you prefer). If you arrange for some other variable to refer to obj, then it's also available via that variable.

One way to arrange for some other variable to point to obj is to take a field of obj which is defined as a function/closure, and invoke it using "method" syntax, as in obj.foo(). That means that inside the body of foo, for the duration of that invocation, the special variable this will refer to obj. Therefore code within that function can access obj.a via this.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.

Upvotes: 5

Related Questions