Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

HTML5 Canvas public var?

Explorer ,
Feb 05, 2018 Feb 05, 2018

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;

284
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 05, 2018 Feb 05, 2018

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;

Translate
LEGEND ,
Feb 05, 2018 Feb 05, 2018

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 06, 2018 Feb 06, 2018

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 06, 2018 Feb 06, 2018

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.

https://javascriptplayground.com/es5-getters-setters/

Working with objects - JavaScript | MDN

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 06, 2018 Feb 06, 2018
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines