Copy link to clipboard
Copied
In HTML5 Canvas mode I am trying to access a child var inside an object similar to how I would in AS3 with public var. Any advice on this?
AS3
Parent object [root]
trace(childObj.age); // 10
Child object [childObj]
public var age:Number = 10;
JS
var childObject = this.childObj;
console.log(childObject.age); //undefined
Child object [childObj]
var age = 10;
1 Correct answer
To oversimplify a bit, child functions can read parent variables, but not the other way around. When a child function exits, its variables cease to exist (usually).
If you want to store data in an object that can be accessed externally, you have to assign it as a property instead.
this.age = 10;
Copy link to clipboard
Copied
To oversimplify a bit, child functions can read parent variables, but not the other way around. When a child function exits, its variables cease to exist (usually).
If you want to store data in an object that can be accessed externally, you have to assign it as a property instead.
this.age = 10;
Copy link to clipboard
Copied
Wonderful!
This was just the reply I was looking for. I don't suppose this is possible to make a child function trigger by using a 'once' or 'change' event on the property?
Copy link to clipboard
Copied
Umm, is someone going to be changing property values without you knowing about it? I suppose if you really want to over-engineer things you could define a getter method that would then do whatever you want when a property value is changed.
Copy link to clipboard
Copied
So my project uses a bunch of child objects with varying values which need to be changed based on data which is downloaded from a database.
This data is then used for functions within the child object.
I am finding that the this.age property can be written but not read by the child object
Thanks for the resources I will look into them.

