I have a class that starts like this:
function event()
{
this.event_id = null;
and then it all gets populated with an init function.
In another part of my script I can not access these variables.
If I run this code:
console.log(my_event);
console.log(my_event.event_id);
For the first line the console gives me a nice representation of the event object with the event_id populated, but for the second line it gives me null.
What’s going on? The variable should be public right?
Many thanks for any help.
When you
console.loga non-scalar value, such as an object, its actual state is retrieved only at the time when you expand to see the object properties in chrome developer tools console. Short arrays or array-likes of < 100 items will automatically expand though the state is usually (my test results have varied on this, maybe I’m just seeing things) not immediately retrieved in that case either.So if you get
nullfromconsole.log(my_event.event_id);, then it was in factnullat the time thatconsole.logexecuted. The results ofconsole.log(my_event);depend on when you expand the logged object in console. If you made an ajax request that modifies theevent_idproperty and the request was completed by the time you expanded the object in console, it will show the changed value.Also, every object property in javascript is “public”.