-1

Hi i am newb to javascript when i was trying write a code i encounter this problem

var k = { 
  
    sam: {
      b: k.bar.x,
      },
  
    bar: {
      x: "Hi",
     },
  };

I dono how to access that bar.x property. I tried using getter and setters.

I know it can be accessed if i use b:this.k.bar.x . But this bad way to access it.

Please clarify my doubt. Is my understanding is wrong or my code is wrong. Thank you

Amerrnath
  • 2,227
  • 4
  • 22
  • 33
  • 2
    You can't use object properties that way. Create the object with all constant fields and then add dynamic ones *afterwards*. – Sirko Oct 10 '14 at 10:07
  • 2
    No, it cannot be accessed using `b: this.k.bar.x`. –  Oct 10 '14 at 11:23

1 Answers1

-2

Maybe this will help, taken straight from the MDN website.

var myHonda = {color: "red", wheels: 4, engine: {cylinders: 4, size: 2.2}};

MDN Objects ref

I can't post pictures yet, but after writing that javascript, I opened my console and typed: myHonda.engine.cylinders and it returned 4. So that is how it works, to clarify.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user3725340
  • 33
  • 1
  • 1
  • 6