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

Help me with this Code??

New Here ,
Nov 03, 2015 Nov 03, 2015

I'm currently on AS3  for Flash and whenever i run this code:

function printMyName()

{

  trace (this.name);

}

var myCountry = new Object();

myCountry.name = "United States";

myCountry.printName = printMyName;

myCountry.myCity = new Object();

myCountry.myCity.name = "Phelan";

myCountry.myCity.printName = printMyName;

myCountry.myCity.myStreet = new Object();

myCountry.myCity.myStreet.name = "Tim Buck Two";

myCountry.myCity.myStreet.printName = printMyName;

myCountry.printName();

myCountry.myCity.printName();

myCountry.myCity.myStreet.printName();


In the output it says:

root1

root1

root1

When it needs to be:

United States

Phelan

Tim Buck Two

TOPICS
ActionScript
300
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 ,
Nov 03, 2015 Nov 03, 2015
LATEST

The way you have that written, 'this' will always refer to the current timeline.  Here is one way to rewrite it...

function printMyName(src:Object)

{

  trace (src.name);

}

var myCountry = new Object();

myCountry.name = "United States";

myCountry.printName = printMyName(myCountry);

myCountry.myCity = new Object();

myCountry.myCity.name = "Phelan";

myCountry.myCity.printName = printMyName(myCountry.myCity);

myCountry.myCity.myStreet = new Object();

myCountry.myCity.myStreet.name = "Tim Buck Two";

myCountry.myCity.myStreet.printName = printMyName(myCountry.myCity.myStreet);

myCountry.printName;

myCountry.myCity.printName;

myCountry.myCity.myStreet.printName;

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