Avatar billede paal_dk Nybegynder
18. november 2004 - 16:27 Der er 7 kommentarer

AS2 class instance properties behaves like static

Can anyone explain why a.arr and b.arr in the below example references to the same variable???
The conclusion seems to be that that if you don't initialize object properties in the constructor, some weird sideeffects may happen.

Kind regards,
P.

PS: Svar er ok på dansk

-------------
Testclass.as:
class Testclass {
  public var arr = new Array();
  function Testclass() {
      arr.push("asd");
  }
}
-------------
Frame 1:
var a = new Testclass();
var b = new Testclass();
b.arr.push("test");
trace( a.arr );
trace( b.arr )
--------------
output:
asd,asd,test
asd,asd,test
--------------
Avatar billede barklund Nybegynder
18. november 2004 - 16:46 #1
You must instantiate complex datatypes to be stored in non-static members in the class constructor - instead of doing it in the variable declaration:

-------------
Testclass.as:
class Testclass {
  public var arr:Array;
  function Testclass() {
      arr = new Array();
      arr.push("asd");
  }
}
-------------
Frame 1:
var a = new Testclass();
var b = new Testclass();
b.arr.push("test");
trace( a.arr );
trace( b.arr )
--------------
output:
asd,asd,test
asd,asd,test
--------------

Otherwise they both inherit the same reference to the exact same object from the prototype chain. This goes for all complex datatypes (anything other than number, string and boolean).

Compare it to the old ActionScript 1 notation:

Testclass = function() {
    this.arr.push("asd");
}
Testclass.prototype.arr = new Array();

Your above AS2 class will be compiled to the same as above anyway. As you can see, there is only 1 array - each instance will just work on individual references to this single array instance. If you on the other hand instantiated the array in the constructor, multiple arrays will exist and it will work as expected. :)

This discussion has also been raised many times on the FlashCoders list (http://chattyfig.figleaf.com/) - just FYI :)

--
Morten Barklund
Avatar billede mat Nybegynder
18. november 2004 - 16:46 #2
Selvom det er lidt sært så:

"The properties defined for the class are not defined specifically for instances when they are created, but are instead added to the prototype object as shared properties for all instances created. They become specific to the instance when defined within the constructor (or some other method if not handled through the constructor specifically).

This is a very important concept to understand when dealing with object and array properties. Because they are assigned to the prototype and not instances, you will need to specifically define the property within the constructor of the class or else methods of that class would be accessing the common object or array within the prototype. Here's an example where a single array property gets used by two instances."

http://www.kirupa.com/developer/oop2/AS2OOPClassStructure3.htm

damn prototype languages...
Avatar billede mat Nybegynder
18. november 2004 - 16:47 #3
hmrf to late...
Avatar billede barklund Nybegynder
18. november 2004 - 17:01 #4
Wee, jeg vandt igen ;)
Avatar billede mat Nybegynder
18. november 2004 - 17:03 #5
I'll be back ... [tilsæt selv østrigsk accent]
Avatar billede paal_dk Nybegynder
19. november 2004 - 10:00 #6
Gode og brugbare svar fra begge. Foretrækker dog Mat's da det er det mest direkte svar på hvad der foregår uden en indledning om at man skal huske at initialisere i konstruktoren osv. osv. som jeg allerede har konstateret i spm. No offence Barklund, og et for langt svar er bedre end et for kort. Du var først, så hvis du vil have points kan du bare skrive et svar ind.

Tak for henvisningerne til figleaf og kirupa.

PS: Den beskrevne funktionalitet er en afvigelse fra NS' ECMA-262-4 standart http://www.mozilla.org/js/language/es4/, men det er sikkert også nævnt på figleaf :-). Hvis man ser den igennem er der *en masse* ting MM har udeladt, synd. Ser meget frem til næste version, maelstroem, hvor de ser ud til, uden at love for meget, at tage standarten mere seriøst:

"we plan to evolve ActionScript toward full compliance with the latest ECMAScript version, called Edition 4. This will bring new power to ActionScript." (http://www.macromedia.com/devnet/logged_in/wanbar_maelstrom.html)

/Pål
Avatar billede barklund Nybegynder
19. november 2004 - 13:20 #7
Ja, det faktum at du selv havde konstateret at det skulle gøres i constructoren, så jeg først bagefter. Men jeg kaster et svar - vi kan jo også bare dele pointene ;)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester