3

Why can't i do something like this in javascript?

var big = { a:1, b:2, c:3, d:big.a }

How can an object's one property access another?

//--------------------------------edited as below ----------------------------------

Thanks for all the answers, now i found another question:

when i do this:

var big =  {
        a : 1,
        b : 2,
        c : 3,
        d : this.a
    }
   console.log(big.d);

It's undefined

However, when i do this:

var big =  {
        a : 1,
        b : 2,
        c : 3,
        d : function(){console.log(this.a)}
    }

 big.d();

It logs out 1

I wonder what's going on here, is it because it is a function in the second code somehow makes the 'this' accessible?

Thanks.

kikkpunk
  • 1,287
  • 5
  • 22
  • 34

4 Answers4

1

you can do this

var big = { a:1, b:2, c:3};
big.d = big.a;

In your code when you are using d:big.a it is still not defined i'e. big is not defined

ashishmaurya
  • 1,196
  • 10
  • 18
1

Try this

var big = new function ()
        {
            this.a = 1;
            this.b = 2;
            this.c = 3;
            this.d = this.a;
        }

        alert(big.a);
        alert(big.b);
        alert(big.c);
        alert(big.d);

Hope this helps.

phoenixinobi
  • 144
  • 1
  • 8
  • Hi @phoenixinobi, ths, may i ask why you made it into a function other than the original object? what's the difference here? – kikkpunk May 18 '14 at 15:18
  • The one that you are using is just an object literal. When you define object literals, members are assigned immediately. So you are getting undefined because you can't have access to "this" while defining the object literal. Functions are also immediately assigned to the members but the way javascript handles a function is different. Functions are only executed when called. So even though you have defined big.d as a function, the code inside it don't get executed until you called it. That's the reason you have access to this.a inside the big.d function. – phoenixinobi May 19 '14 at 04:14
  • If you are only after the ease of declaration, you can use my example above. I also thought of making big.d a function but realized that it's not pretty and might as well declare big as an object and not just an object literal. You use "function" to declare objects in javascript. – phoenixinobi May 19 '14 at 04:15
0

Since the variable big to get big.a hasn't been declared yet when you actually declare big. In this case you should inject the object d afterwards.

var big = { a:1, b:2, c:3 };
big.d = big.a;
Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39
0

You can't do it in the object literal. This is because the right hand side of the = is evaluated before the assignment takes place. Javascript first creates an object with the properties given and then assigns it to the variable. Therefore, at the point when you attempt to access big.a, the assignment has not taken place, so big is undefined and you get an error because you cannot access the properties of undefined.

You need to assign big.d after the assignment:

var big = { a:1, b:2, c:3};
big.d = big.a;
lonesomeday
  • 233,373
  • 50
  • 316
  • 318