Re: [Flashcoders] clean scripting
In AS2 it does (and has been discussed here in the past), in AS3 it doesn't. - Original Message - From: Jesse Graupmann [EMAIL PROTECTED] To: 'Flash Coders List' flashcoders@chattyfig.figleaf.com Sent: Wednesday, March 12, 2008 5:38 AM Subject: RE: [Flashcoders] clean scripting Muzak, Maybe I missed something, but instance2 in your example has no data. Only static members can be shared across all instances - did you mean... class MyClass { private static var myArray:Array = new Array(); public function addItem(item:Object) { MyClass.myArray.push(item); } public function get data():Array { return MyClass.myArray; } } regards, Jesse ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] clean scripting
Sorry for being away for awhile and just testing that out now. So yea... WTF? I can't believe after my years of AS2 coding that it would have taken me this long to notice. Well, good thing for AS3. // NOT SHARED public var myArray:Array; public function MyClass() { myArray = []; } // SHARED public var myArray:Array = new Array(); public function MyClass() { } Cheers, Jesse -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Muzak Sent: Wednesday, March 12, 2008 2:04 AM To: Flash Coders List Subject: Re: [Flashcoders] clean scripting In AS2 it does (and has been discussed here in the past), in AS3 it doesn't. - Original Message - From: Jesse Graupmann [EMAIL PROTECTED] To: 'Flash Coders List' flashcoders@chattyfig.figleaf.com Sent: Wednesday, March 12, 2008 5:38 AM Subject: RE: [Flashcoders] clean scripting Muzak, Maybe I missed something, but instance2 in your example has no data. Only static members can be shared across all instances - did you mean... class MyClass { private static var myArray:Array = new Array(); public function addItem(item:Object) { MyClass.myArray.push(item); } public function get data():Array { return MyClass.myArray; } } regards, Jesse ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
So yea...WTF? I can't believe after my years of AS2 coding that it would have taken me this long to notice. I think I muttered those exact words. Bizzare behavior. Though I don't think I have ever run into it. Someone taught me long ago not to initialize class variables in their definitions. So I just never have done that. Really good to know though. Dave - Head Developer http://www.blurredistinction.com Adobe Community Expert http://www.adobe.com/communities/experts/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
Here's my understanding of the reason behind this: AS2 is basically just syntactic sugar over AS1, and gets compiled down to the same thing. When defining a class you're actually defining things on the prototype, so doing this: -- class MyClass { public var myArray:Array; public function MyClass() { myArray = []; } public function push(o:Object):Void { myArray.push(o); } } -- would be the same as: -- MyClass = function() { this.myArray = []; } MyClass.prototype.push = function(o) { this.myArray.push(o); } -- If you set the value of the member variable at declaration: -- public var myArray:Array = new Array(); -- It turns into this: -- MyClass.prototype.myArray = new Array(); -- Since you have just assigned an array instance to the prototype of the class, that gets shared between all instances of the class (basically, the value you set there is the initial value given to the myArray member of the class on instantiation). Well, assuming my understanding is correct. :) -Andy On Wed, Mar 12, 2008 at 2:02 PM, Dave Mennenoh [EMAIL PROTECTED] wrote: So yea...WTF? I can't believe after my years of AS2 coding that it would have taken me this long to notice. I think I muttered those exact words. Bizzare behavior. Though I don't think I have ever run into it. Someone taught me long ago not to initialize class variables in their definitions. So I just never have done that. Really good to know though. Dave - Head Developer http://www.blurredistinction.com Adobe Community Expert http://www.adobe.com/communities/experts/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
MyClass.prototype.myArray = new Array(); That is indeed correct. It should be in the archives somewhere (if they go back that long). regards, Muzak - Original Message - From: Andy Herrman [EMAIL PROTECTED] To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Wednesday, March 12, 2008 9:02 PM Subject: Re: [Flashcoders] clean scripting Here's my understanding of the reason behind this: AS2 is basically just syntactic sugar over AS1, and gets compiled down to the same thing. When defining a class you're actually defining things on the prototype, so doing this: -- class MyClass { public var myArray:Array; public function MyClass() { myArray = []; } public function push(o:Object):Void { myArray.push(o); } } -- would be the same as: -- MyClass = function() { this.myArray = []; } MyClass.prototype.push = function(o) { this.myArray.push(o); } -- If you set the value of the member variable at declaration: -- public var myArray:Array = new Array(); -- It turns into this: -- MyClass.prototype.myArray = new Array(); -- Since you have just assigned an array instance to the prototype of the class, that gets shared between all instances of the class (basically, the value you set there is the initial value given to the myArray member of the class on instantiation). Well, assuming my understanding is correct. :) -Andy On Wed, Mar 12, 2008 at 2:02 PM, Dave Mennenoh [EMAIL PROTECTED] wrote: So yea...WTF? I can't believe after my years of AS2 coding that it would have taken me this long to notice. I think I muttered those exact words. Bizzare behavior. Though I don't think I have ever run into it. Someone taught me long ago not to initialize class variables in their definitions. So I just never have done that. Really good to know though. Dave - Head Developer ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] clean scripting
Actually, I've seen that the values are often set quicker when initilaised in the contructor rather than against class members , but I think it looks tidier the way you've done it... imo -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Allandt Bik-Elliott (Receptacle) Sent: 11 March 2008 17:41 To: flashcoders Subject: [Flashcoders] clean scripting hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? so for instance, would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties = new CommonProperties(); private var commonY:uint = cp. commonY; private var commonCopy:String = cp.commonCopy; private static var title:String = Title; private static var subtitle:String = Subtitle; public function Class() { myFunc1(); } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which works fine but is a little messy at the class level or would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties; private var commonY:uint; private var commonCopy:String private static var title:String private static var subtitle:String ; public function Class() { setVars(); myFunc1(); } private function setVars() { cp = new CommonProperties(); commonY = cp. commonY; commonCopy = cp.commonCopy; title = Title; subtitle = Subtitle; } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which seems cleaner but is more round the houses. thanks in advance a ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders __ This e-mail has been scanned for viruses by the Virtual Universe e-mail security system - powered by MessageLabs. http://www.virtual-universe.net __ The contents of this email (which include its attachments) are confidential and may be subject to legal privilege and protected by copyright. If you are not the intended recipient any use, copying or disclosure of this e-mail to any third party is strictly forbidden by the sender and we reserve all rights and remedies against any person or entity making any such unauthorised use. If you have received this email in error, please contact the sender immediately by telephone or return the email to the sender and then delete this email and any copies of it on your system. Virtual Universe Limited may monitor the contents of emails sent and received via its network for viruses and to ensure the lawful and authorised use of its systems. Virtual Universe Limited will not be held responsible for any damage caused by viruses which may be transmitted upon receipt of this email or the opening of any attachment thereto. Any views or opinions presented in this email are solely those of th! e author and do not necessarily represent those of Virtual Universe Limited. Virtual Universe Limited is a company established under the laws of England and Wales with registered number 03064568 and has its registered office at 1 Regent Street, London, SW1Y 4NW and principal place of business at 28-39 The Quadrant, 135 Salusbury Road, London NW6 6RJ, United Kingdom. It is registered for VAT in the United Kingdom with number GB877113217.
Re: [Flashcoders] clean scripting
Not sure if this is correct for AS3, but I was under the impression that there was an actual performance penalty to doing things the first way (object creation and assignment in the class definition rather than in the methods or the constructor). Still, I like doing things the 2nd way, if only because it's habit for me at this point. In my own twisted little mind it also prevents me from experiencing the Where the hell did I define that syndrome --- I always know where I set the initial values of my class variables. Would be interested to know if my memory of [some book I can't remember the name of] told me right. jer ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
Either way to me is a non-issue. However, it is an issue if I want to implement lazy instantiation. That is, instantiating objects only right before you need them. Thus, in your first example, the bulk of instantiation occurs up-front. Lazy instantiation http://www.javaworld.com/javaworld/javatips/jw-javatip67.html -mL Allandt Bik-Elliott (Receptacle) wrote: hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? so for instance, would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties = new CommonProperties(); private var commonY:uint = cp. commonY; private var commonCopy:String = cp.commonCopy; private static var title:String = Title; private static var subtitle:String = Subtitle; public function Class() { myFunc1(); } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which works fine but is a little messy at the class level or would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties; private var commonY:uint; private var commonCopy:String private static var title:String private static var subtitle:String ; public function Class() { setVars(); myFunc1(); } private function setVars() { cp = new CommonProperties(); commonY = cp. commonY; commonCopy = cp.commonCopy; title = Title; subtitle = Subtitle; } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which seems cleaner but is more round the houses. thanks in advance a ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Notice of confidentiality: The information contained in this e-mail is intended only for the use of the individual or entity named above and may be confidential. Should the reader of this message not be the intended recipient, you are hereby notified that any unauthorized dissemination, distribution or reproduction of this message is strictly prohibited. If you have received this message in error, please advise the sender immediately and destroy the e-mail. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
they're both eager, though, aren't they? the only difference is that everything is instantiated in the first method of the class rather than in the class head a On 11 Mar 2008, at 18:10, Mark Lapasa wrote: Either way to me is a non-issue. However, it is an issue if I want to implement lazy instantiation. That is, instantiating objects only right before you need them. Thus, in your first example, the bulk of instantiation occurs up- front. Lazy instantiation http://www.javaworld.com/javaworld/javatips/ jw-javatip67.html -mL Allandt Bik-Elliott (Receptacle) wrote: hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? so for instance, would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties = new CommonProperties(); private var commonY:uint = cp. commonY; private var commonCopy:String = cp.commonCopy; private static var title:String = Title; private static var subtitle:String = Subtitle; public function Class() { myFunc1(); } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which works fine but is a little messy at the class level or would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties; private var commonY:uint; private var commonCopy:String private static var title:String private static var subtitle:String ; public function Class() { setVars(); myFunc1(); } private function setVars() { cp = new CommonProperties(); commonY = cp. commonY; commonCopy = cp.commonCopy; title = Title; subtitle = Subtitle; } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which seems cleaner but is more round the houses. thanks in advance a ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Notice of confidentiality: The information contained in this e-mail is intended only for the use of the individual or entity named above and may be confidential. Should the reader of this message not be the intended recipient, you are hereby notified that any unauthorized dissemination, distribution or reproduction of this message is strictly prohibited. If you have received this message in error, please advise the sender immediately and destroy the e-mail. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] clean scripting
I go for the second option. Instanciate at the point you need it, and clear if no longer needed. It also keep memory use limited. HTH C -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Allandt Bik-Elliott (Receptacle) Verzonden: dinsdag 11 maart 2008 18:41 Aan: flashcoders Onderwerp: [Flashcoders] clean scripting hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? so for instance, would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties = new CommonProperties(); private var commonY:uint = cp. commonY; private var commonCopy:String = cp.commonCopy; private static var title:String = Title; private static var subtitle:String = Subtitle; public function Class() { myFunc1(); } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which works fine but is a little messy at the class level or would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties; private var commonY:uint; private var commonCopy:String private static var title:String private static var subtitle:String ; public function Class() { setVars(); myFunc1(); } private function setVars() { cp = new CommonProperties(); commonY = cp. commonY; commonCopy = cp.commonCopy; title = Title; subtitle = Subtitle; } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which seems cleaner but is more round the houses. thanks in advance a ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- No virus found in this incoming message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 No virus found in this incoming message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 No virus found in this outgoing message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
To clarify, this difference only matters in the case of static class members. Anything non-static gets initialized at constructor-time regardless -- the difference is syntax only. On 3/11/08, Cor [EMAIL PROTECTED] wrote: I go for the second option. Instanciate at the point you need it, and clear if no longer needed. It also keep memory use limited. HTH C -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Allandt Bik-Elliott (Receptacle) Verzonden: dinsdag 11 maart 2008 18:41 Aan: flashcoders Onderwerp: [Flashcoders] clean scripting hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? so for instance, would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties = new CommonProperties(); private var commonY:uint = cp. commonY; private var commonCopy:String = cp.commonCopy; private static var title:String = Title; private static var subtitle:String = Subtitle; public function Class() { myFunc1(); } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which works fine but is a little messy at the class level or would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties; private var commonY:uint; private var commonCopy:String private static var title:String private static var subtitle:String ; public function Class() { setVars(); myFunc1(); } private function setVars() { cp = new CommonProperties(); commonY = cp. commonY; commonCopy = cp.commonCopy; title = Title; subtitle = Subtitle; } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which seems cleaner but is more round the houses. thanks in advance a ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- No virus found in this incoming message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 No virus found in this incoming message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 No virus found in this outgoing message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Cory Petosky : Lead Developer : PUNY 1618 Central Ave NE Suite 130 Minneapolis, MN 55413 Office: 612.216.3924 Mobile: 240.422.9652 Fax: 612.605.9216 http://www.punyentertainment.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
Allthough the following no longer seems to apply in AS3, it might be good to know. In AS2, if you have the following class: class MyClass { private var myArray:Array = new Array(); public function addItem(item:Object) { myArray.push(item); } public function get data():Array { return myArray; } } The myArray class member will be shared across ALL instances of the class. var instance1:MyClass = new MyClass(); var instance2:MyClass = new MyClass(); instance1.addItem(Hello World); trace(instance1.data); trace(instance2.data); //output: Hello World Hello World regards, Muzak ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
thanks for your input everyone i've gone for the second option because i can template it out better and it looks way neater (plus i can still keep all of my declarations in one place, the setVars() method) thanks again here's a copy of my template if you'd like it //code: package com.receptacle.timeline { //package imports internal class Class { // class variable declarations // constructor public function Constructor() { setVars(); } // set class variables private function setVars() { } } } //code best a On 11 Mar 2008, at 22:20, Cory Petosky wrote: To clarify, this difference only matters in the case of static class members. Anything non-static gets initialized at constructor-time regardless -- the difference is syntax only. On 3/11/08, Cor [EMAIL PROTECTED] wrote: I go for the second option. Instanciate at the point you need it, and clear if no longer needed. It also keep memory use limited. HTH C -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Allandt Bik-Elliott (Receptacle) Verzonden: dinsdag 11 maart 2008 18:41 Aan: flashcoders Onderwerp: [Flashcoders] clean scripting hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? so for instance, would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties = new CommonProperties(); private var commonY:uint = cp. commonY; private var commonCopy:String = cp.commonCopy; private static var title:String = Title; private static var subtitle:String = Subtitle; public function Class() { myFunc1(); } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which works fine but is a little messy at the class level or would you...: package com.receptacle.timeline { //package imports import flash.display.Sprite; internal class Class extends Sprite { // class variable declarations private var cp:CommonProperties; private var commonY:uint; private var commonCopy:String private static var title:String private static var subtitle:String ; public function Class() { setVars(); myFunc1(); } private function setVars() { cp = new CommonProperties(); commonY = cp. commonY; commonCopy = cp.commonCopy; title = Title; subtitle = Subtitle; } private function myFunc1() { trace (function ran); trace (commonY is +commonY); trace (commonCopy is +commonCopy); trace (title is +title); trace (subtitle is +subtitle); } } } which seems cleaner but is more round the houses. thanks in advance a ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- No virus found in this incoming message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 No virus found in this incoming message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 No virus found in this outgoing message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1325 - Release Date: 11-3-2008 13:41 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- Cory Petosky : Lead Developer : PUNY 1618 Central Ave NE Suite 130 Minneapolis, MN 55413 Office: 612.216.3924 Mobile:
Re: [Flashcoders] clean scripting
I'm going to chime in here. Lazy Instantiation is an irrelevant argument when you're instantiating in the constructor. What you perceive as cleaner is merely philosophical. var container:Sprite = new Sprite(); vs var container:Sprite; public function ClassName() { container = new Sprite(); } I think the single line version is cleaner. In fact, I would argue that instantiating classes in their variable declaration is a very specific way of making it extremely clear that those variables are being made available immediately to the class, and anything that does not have a definition is going to be lazily instantiated later. I'm a big fan of this type of coding practice where the meaning is derived from the style. My 2 cents, Steven ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] clean scripting
Muzak, Maybe I missed something, but instance2 in your example has no data. Only static members can be shared across all instances - did you mean... class MyClass { private static var myArray:Array = new Array(); public function addItem(item:Object) { MyClass.myArray.push(item); } public function get data():Array { return MyClass.myArray; } } regards, Jesse -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Muzak Sent: Tuesday, March 11, 2008 4:51 PM To: Flash Coders List Subject: Re: [Flashcoders] clean scripting Allthough the following no longer seems to apply in AS3, it might be good to know. In AS2, if you have the following class: class MyClass { private var myArray:Array = new Array(); public function addItem(item:Object) { myArray.push(item); } public function get data():Array { return myArray; } } The myArray class member will be shared across ALL instances of the class. var instance1:MyClass = new MyClass(); var instance2:MyClass = new MyClass(); instance1.addItem(Hello World); trace(instance1.data); trace(instance2.data); //output: Hello World Hello World regards, Muzak ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
Yes, you might be missed something, Jesse. I got the same result as Muzak did. // ActionScript 2.0 // frame action: var instance1:MyClass = new MyClass(); var instance2:MyClass = new MyClass(); instance1.addItem(Hello World); trace(instance1.data); // Output: Hello World trace(instance2.data); // Output: Hello World _ Jesse Graupmann wrote: Muzak, Maybe I missed something, but instance2 in your example has no data. Only static members can be shared across all instances - did you mean... -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Muzak Sent: Tuesday, March 11, 2008 4:51 PM To: Flash Coders List Subject: Re: [Flashcoders] clean scripting Allthough the following no longer seems to apply in AS3, it might be good to know. In AS2, if you have the following class: class MyClass { private var myArray:Array = new Array(); public function addItem(item:Object) { myArray.push(item); } public function get data():Array { return myArray; } } The myArray class member will be shared across ALL instances of the class. var instance1:MyClass = new MyClass(); var instance2:MyClass = new MyClass(); instance1.addItem(Hello World); trace(instance1.data); trace(instance2.data); //output: Hello World Hello World Good luck, -- Fumio Nonaka http://www.FumioNonaka.com/ My bookshttp://www.FumioNonaka.com/Books/index.html Flash communityhttp://F-site.org/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] clean scripting
I agree with Steven. You may want to put your variable declarations in another function when they either get suuuper messy or you need more data (ie. stage.stageWidth). Going back to style... typically I like to run an INIT() in the constructor and then break down the tasks into INIT_name(). It cleans up the top pretty well and makes the code very easy to read/understand. package { import flash.display.Sprite; import flash.events.MouseEvent; public class MyClass extends Sprite { public static var prop:Number = 0; public var solid:Sprite; public function MyClass() { INIT(); } // ___ EVENTS private function click ( e:MouseEvent ):void { //... } // ___ INITIALIZE private function INIT ():void { INIT_clips(); INIT_vars(); } private function INIT_clips ():void { solid.addEventListener ( MouseEvent.CLICK, click ); } private function INIT_vars ():void { // ... } } } 2 more cents, Jesse -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Steven Sacks Sent: Tuesday, March 11, 2008 6:49 PM To: Flash Coders List Subject: Re: [Flashcoders] clean scripting I'm going to chime in here. Lazy Instantiation is an irrelevant argument when you're instantiating in the constructor. What you perceive as cleaner is merely philosophical. var container:Sprite = new Sprite(); vs var container:Sprite; public function ClassName() { container = new Sprite(); } I think the single line version is cleaner. In fact, I would argue that instantiating classes in their variable declaration is a very specific way of making it extremely clear that those variables are being made available immediately to the class, and anything that does not have a definition is going to be lazily instantiated later. I'm a big fan of this type of coding practice where the meaning is derived from the style. My 2 cents, Steven ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] clean scripting
- Original Message - From: Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] To: flashcoders flashcoders@chattyfig.figleaf.com Sent: Tuesday, March 11, 2008 5:41 PM Subject: [Flashcoders] clean scripting hi just a semantic question really when writing your classes, would you only declare variables in the class and assign variables later or would you assign values straight away if you had them? I generally avoid initialisation in the class member declarations and have the constructor do very little, usually just calling an init() function (loadVars equivalent). You can then the use the init function to re-initialise an instance. Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders