Re: [Flashcoders] Re: Newbie AS3 question
Yes indeed! You can usually find an alternative approach to solving the problem than using the singleton pattern, but it is distinctly different to using static methods. Glad to hear it works the way it does in other languages at least. Spike On 10/29/05, A.Cicak [EMAIL PROTECTED] wrote: Problem with JasterXL's class is initialization. For example if you had to initialize something in that class you should make static method called InitCurrencyFormatter(). But when to call it? If other classes are using CurrencyFormatter than you should ensure to call InitCurrencyFormatter() before construction of any class which uses it is called. But what if InitCurrencyFormatter also depends on some other class which is made just with static methods (like JasterXL's). In that case that other class would have to have InitOtherClass() and it also should be called before other objects are constructed and before InitCurrencyFormatter. Now imagine 20 classes like that and thousands of lines of code depending on them, it would be almost impossible to keep track when to initialize any of these classes, and it would be very error prone (you change order of execution and you start using uninitialized objects). If you make these classes singletons you don't have that problem, because when you call getInstance() if object was not initialized getInstance will call new and that will call its constructor ( which now replaces InitCurrencyFormatter, InitOtherClass, etc..) and it will ensure that all objects are initialized at time of their use, and you do not have to worry about order of execution. Spike [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Sure, Here's a slightly more complete implementation of that last example: public class CurrencyFormatter { private static formatter:CurrencyFormatter; public function getInstance():CurrencyFormatter { // use ExternalInterface or IP lookup or whatever to determine locale if (locale == UK) { formatter = new UKCurrencyFormatter(); } else { formatter = new USCurrencyFormatter(); } return formatter; } class USCurrencyFormatter extends CurrencyFormatter { public formatValue(val:Number) { // very simplistic formatting return $ + String(val); } } class UKCurrencyFormatter extends CurrencyFormatter { public formatValue(val:Number) { // very simplistic formatting return £ + String(val); } } } Let me know if that explains it a bit better. Spike On 10/29/05, JesterXL [EMAIL PROTECTED] wrote: Can you elaborate? Why wouldn't the static class work in that case? - Original Message - From: Spike [EMAIL PROTECTED] To: Flashcoders mailing list flashcoders@chattyfig.figleaf.com Sent: Friday, October 28, 2005 9:54 PM Subject: Re: [Flashcoders] Newbie AS3 question ok, That's just a static class. Like I said, there's a subtle but important difference between singleton and a static class. Here's another example. You have a requirement to provide a currency formatter. One way to do this is to create a singleton that returns a different currency formatter depending on which locale you are in. So in the class you would have something like this (Omitting method declarations for simplicty): public class CurrencyFormatter { class USCurrencyFormatter extends CurrencyFormatter { } class UKCurrencyFormatter extends CurrencyFormatter { } } Now if I call CurrencyFormatter.getInstance() it gives me the correct formatter for my locale. With your static class approach you have to check in every method what the locale is and handle it accordingly. That's fine for one or two locales, but if you want to handle 20, it gets pretty ugly. You can solve the problem in other ways of course, but it does demonstrate the difference between static classes and the singleton pattern. The singleton pattern offers you a lot more possibilities. Spike On 10/29/05, JesterXL [EMAIL PROTECTED] wrote: To clarify: class ServerConnection { private static var url; private static var port; private static var socket; public static function connect(p_url, p_port) { url = p_url; port = p_port; socket = new Socket(); socket.connect(url, port); } public static function getData() { // Simple function that gets something from the server. } } Then to use: ServerConnection.connect(myURL, myPort); - Original Message - From: JesterXL [EMAIL PROTECTED] To: Flashcoders mailing list flashcoders@chattyfig.figleaf.com Sent: Friday, October 28, 2005 9:25 PM Subject: Re: [Flashcoders] Newbie AS3 question Naw, I don't know the exact way Singleton is implemented, hence my long battle with finding clarification. It could of been solved in 10 seconds over a beer, but email sux. I figured Math.abs was the Singleton pattern, and ARP, me, Sho,
Re: [Flashcoders] Re: Newbie AS3 question
I learned programming in Flash and I use this everyday, almost everyline ;) because scope has always been one of the thoughest thing in Flash :-) * cedric thanks ryanm In my opinion (and in the opinion of many much more competent developers than myself), it is always good to be explicit about scope, because it removes any ambiguity from the code, drastically reducing the possibility of expensive and time consuming breakage during maintenance. One more thing worth mentioning, and I don't say this to be rude or as a slight to anyone on this list, but the opinion that the this reference is bad and should be avoided seems to be unilaterally coming from people who learned programming in Flash, while the opinion that scope should be stated explicitly seems to be coming from people with more formal training in software development and experience in (non-Flash) real world development. Personally, I have to give more weight to the opinions of people with more formal training and more varied real-world experience, because there are some things you just can't learn in a year or two of using Flash for web development. There is a reason that experienced developers like to type those extra 5 characters, and it is to save time, money, and the embarrasment of explaining to your boss that you deleted something without fully understanding the implications. ryanm ___ 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
[Flashcoders] Re: Newbie AS3 question
yes, this is true in ActionScript referencing or instantiating a singleton class via a static getInstance method, is a different matter to all methods of a class being static The former is calling an instance method and the latter is calling a static method. Instance methods have access to instance data that is persisted as long as the instance exists, static methods have access only to static data and that data passed in to the method call. Is this not true in ActionScript? Important - This email and any attachments may be confidential. If received in error, please contact us and delete all copies. Before opening or using attachments check them for viruses and defects. Regardless of any loss, damage or consequence, whether caused by the negligence of the sender or not, resulting directly or indirectly from the use of any attached files our liability is limited to resupplying any affected attachments. Any representations or opinions expressed are those of the individual sender, and not necessarily those of the Department of Education Training. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Re: Newbie AS3 question
No need to get hyper about this. The matter stays that this used to be essential in AS1, thus probably why people still like to implicate him in their code. But I agree that putting this in an AS2 Class should be used only when necessary. You do know that it (this.) is being added for you at compile time in AS2, right? Who cares? All that means is that there is no semantic difference between the two. There is a difference for AS3 ! (and this is the correct behaviour) http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#this To call a function defined in a dynamic class, you must use this to invoke the function in the proper scope zwetan ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Re: Newbie AS3 question
In the book of C.Moock, he recommends the redundant use of [this] Sorry, I'm a newbie too but it seems to be a good practice to specify clearly the scope you refer to. It's clearly specified in chapter 6 to be aware about the scope. Essential ActionScript 2.0 O'Reilly Media, Inc, 2004, ISBN 0-596-00652-7 Greetings N -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of zwetan Sent: samedi 29 octobre 2005 15:03 To: 'Flashcoders mailing list' Subject: RE: [Flashcoders] Re: Newbie AS3 question No need to get hyper about this. The matter stays that this used to be essential in AS1, thus probably why people still like to implicate him in their code. But I agree that putting this in an AS2 Class should be used only when necessary. You do know that it (this.) is being added for you at compile time in AS2, right? Who cares? All that means is that there is no semantic difference between the two. There is a difference for AS3 ! (and this is the correct behaviour) http://livedocs.macromedia.com/labs/1/flex/langref/statements.html#this To call a function defined in a dynamic class, you must use this to invoke the function in the proper scope zwetan ___ 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] Re: Newbie AS3 question
The idea is to use it (this) when you have two variables with the same name, usually to distinguish between the class member and a locally declared variable. You must know that :) The compiler won't know to use this or not in those cases, so it is important to use it in those contexts. Actually, the compiler *does* know, it always adds this. to references without explicitly stated scope. Which brings us full circle, all the way back to maintainability. Now, if you have a class, and all of the member functions and variables are referenceless (using assumed scope without the this reference), and all of a sudden, in the middle of a method, you have a this reference because of a potential scope conflict, what does a developer looking at your code 2 years after you've left the job think about it? Does he remove the single this reference, to make it more readable and consistent, thus breaking the method and blowing up the whole project, all without knowing how or why? Martin piped up with I would refactor the method to make it more readable... immediately, which, in this case, would've broken the method (if he removed the reference) and possibly the whole class, which is exactly how these sort of expensive, time consuming, and difficult to troubleshoot problems come up. On the other hand, if the original developer used the this reference every time, it would already be consistent, readable, and explicit about scope. The other question is, would Martin (not to pick on Martin, but he was vocal about it, so I use him as an example) take a class that stated scope explicitly and remove all the this references, possibly breaking the class in the process? It sounds like it, from his post. The question is, is that a good practice or a bad practice, and does that make him a good developer, or a bad one, given the potential for expensive and time consuming breakage caused solely by his dislike for the keyword this? In my opinion (and in the opinion of many much more competent developers than myself), it is always good to be explicit about scope, because it removes any ambiguity from the code, drastically reducing the possibility of expensive and time consuming breakage during maintenance. One more thing worth mentioning, and I don't say this to be rude or as a slight to anyone on this list, but the opinion that the this reference is bad and should be avoided seems to be unilaterally coming from people who learned programming in Flash, while the opinion that scope should be stated explicitly seems to be coming from people with more formal training in software development and experience in (non-Flash) real world development. Personally, I have to give more weight to the opinions of people with more formal training and more varied real-world experience, because there are some things you just can't learn in a year or two of using Flash for web development. There is a reason that experienced developers like to type those extra 5 characters, and it is to save time, money, and the embarrasment of explaining to your boss that you deleted something without fully understanding the implications. ryanm ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: Newbie AS3 question
You do know that it (this.) is being added for you at compile time in AS2, right? Who cares? All that means is that there is no semantic difference between the two. Not quite. What it means is that the this is assumed, which is not always what you want. And if you need the reference sometimes and other times it is extraneous, why not use it all the time for the sake of consistency? ryanm ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Re: Newbie AS3 question
Hey ryanm, Imagine this: class MyClass { private var myName:String; public function MyClass(myName:String) { this.myName = myName } } Or: class MyClass { public function MyClass(myEventDispatcher) { var myListener = new Object(); myListener.controler = this; myListener.click = function (eventObj) { this.controler.onMyClick(eventObj); } myEventDispatcher.addEventListener(click, myListener); } public function onMyClick (eventObj) { } } Naturally you can use the Delegate object instead of creating an anonymous function, but the fact states that those are moments you absolutely need to use this and good practice is about using them when you need them / When you want to write them everywhere, it's really the programmers choice to write it as he wishes but he should think of future programmers that might go into that code, they might not want to scroll horizontally that much to read the code, it just makes lines shorter and easier to read, if well written. Now you're saying that the compiler adds this everywhere or almost... Well this is more like the pre-compiler's job, and this is why it actually exists. The idea is to write human readable ECMA script that isn't too cluttered, that the pre-compiler can arrange for the compiler to compile. If we didn't have the pre-compiler we'd be writing a lot of code with a lot of this everywhere and we wouldn't be able to forget to put a semi-column at the end of each line :P (a bit like PHP) On the other side, in AS1 when creating your classes it is about 100% necessary to specify this in front of instance properties or methods. Things have changed in AS2 and their closer to what standard OOP programming should look like. If you look in the world of JAVA, you won't find extensive usage of the this statement like you seem to be saying. But it is really the programmer's discretion to use this as he wishes when he doesn't HAVE to use it; nobody should blame someone for using this anytime except if it does something that is not meant to be. When programmers modify code, removing the this statements everywhere when going threw somebody's code like your saying, they should be aware of what this might involve, and it's not the fault of the programmer that put all those this statements, it's the fault of the one that removed them I'd say. Common sense! So I wouldn't really be annoyed by reading your code that has this, everywhere it can be used at. It's ok and so be it. All that to say, if your going to be putting this in front of every class member in AS2 and in AS3 you'll be missing the neat advantage of simplicity. I think it's a question of trusting how Flash will get compiled and how it will run it! Trust ECMA script and know what you're coding, if it's AS1, use lots of this; if your in pure AS2, lighten the number of characters in your file if you may!:) Let's not even mention AS3, since it's even more a question of cleaning up your code and making it human readable and OO. Using this everywhere would ruin the fun of writing AS2 and AS3. And yes there are cases in AS2 where your class method might need this in front of all methods and members it uses; this is mostly the case if you're redirecting methods to other classes with their prototype, thus returning to the world of AS1. Cheers, Fredz./ -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ryanm Sent: October 29, 2005 3:39 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Re: Newbie AS3 question The idea is to use it (this) when you have two variables with the same name, usually to distinguish between the class member and a locally declared variable. You must know that :) The compiler won't know to use this or not in those cases, so it is important to use it in those contexts. Actually, the compiler *does* know, it always adds this. to references without explicitly stated scope. Which brings us full circle, all the way back to maintainability. Now, if you have a class, and all of the member functions and variables are referenceless (using assumed scope without the this reference), and all of a sudden, in the middle of a method, you have a this reference because of a potential scope conflict, what does a developer looking at your code 2 years after you've left the job think about it? Does he remove the single this reference, to make it more readable and consistent, thus breaking the method and blowing up the whole project, all without knowing how or why? Martin piped up with I would refactor the method to make it more readable... immediately, which, in this case, would've broken the method (if he removed the reference) and possibly the whole class, which is exactly how these sort of expensive, time consuming, and difficult to troubleshoot problems come up. On the other hand, if the original developer used the this reference every time, it would already be consistent, readable, and explicit
Re: [Flashcoders] Re: Newbie AS3 question
All that to say, if your going to be putting this in front of every class member in AS2 and in AS3 you'll be missing the neat advantage of simplicity. As it were, the classes I write are rarely self-referential. Properties such as position, visibility, etc, are usually handled elsewhere or by other means rather than directly referring to itself, so you won't find this in my code a lot either. When it does appear it is the rare exception, not the rule, but by the same token, you also won't find referenceless method calls either. I very, very rarely put something like this._visible=false; in a constructor, and usually only because it's a quickie, one-off project that won't need to be maintained, and even if maintenance is required, there is usually only one class to deal with and everything is right there. But when I do something like that, I always use this, just so that it's clear what I'm referring to. You also rarely find _parent references in my code, because usually when I need a reference to the parent, I pass that reference in at creation time and store it as a member variable in the class. If it's not important enough to be a member of the class, then it probably isn't necessary, and if it's necessary, it's important enough to be a member. So really, this is the only ryanm ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: Newbie AS3 question
Of course there exists edge cases where that isnt feasible, but most programs dont implement DES algorithms (to relate this to an earlier post) and a lot of legacy code i have worked with has benefitted from being re-factored. I actually do have classes with methods so large that I had to make a base class containing only that method and then extend it (because of the size limit imposed by Flash), but as was mentioned, they are mostly encryption or compression classes, and are special cases. ryanm ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: Newbie AS3 question
ryanm wrote: You do know that it (this.) is being added for you at compile time in AS2, right? Who cares? All that means is that there is no semantic difference between the two. Not quite. What it means is that the this is assumed, which is not always what you want. You can force class scope by including this, but there's no way to do the opposite. Local (or class) scope always obscures global scope, that's just a fact of life. The only exception is built-in function like trace and eval, which normally obscure everything else. That's probably part of the reason AS3 is more heavily based around namespaces - you can more clearly state what scope you want if everything resides in a unique namespace. That takes us back to the point about whether you should include this everywhere - doing so is akin to including the fully qualified name of every class instead of just importing the package. Which is easier to read? - Robert ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Re: Newbie AS3 question
this can also be used to refer an instance's member variable explicitly. Since scoping rules allow for a local variable (in a method) to be named the same as an instance member variable, this is required to differentiate between the two. (Sorry about the html mail) Scott -Original Message- From: [EMAIL PROTECTED] on behalf of A.Cicak Sent: Fri 28/10/2005 6:32 PM To: flashcoders@chattyfig.figleaf.com Cc: Subject: [Flashcoders] Re: Newbie AS3 question Well, I dont agree, this keyword refers to current class, so its only more typing to include it, and making code less readable. Only reason keyword this exists is if you want to pass reference to current object somewhere, in which case you must use this. To me using this in your code makes you look like wannabe-programmer, :) But I gues its matter of taste. btw, old VB does not have this keyword, and if you were reffering to VB(.NET), it is more OOP and more complex than AS3, so I gues programmers in VB.NET (if there are some, since C# is there) are not wannabe-programmers :) ryanm [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What I don't get is why it needs this.addChild instead of just addChild. I've been sick of the keyword this for a long time and have since avoided it in AS2. Any reason that it needs to be back in for AS3? Maybe because it's one of the most useful scope references ever invented? The fundamental concept that you seem to miss is that addChild is meaningless by itself, it is a method of an object (in proper OOP development), and if you just say addChild, who is adding the child? You need a reference. You could do it like this if you like: class Game extends MovieClip { var world:MovieClip; var bg:MovieClip; function Game(){ var GameReference:Game = this; world = new MovieClip(); GameReference.addChild( world ); bg = new MovieClip(); world.addChild( bg ); } } The point is, you shouldn't use functions that aren't attached to objects, it's bad form, and it's thoroughlly confusing to people who later have to maintain your code. Besides, it makes you look like one of those wannabe-programmer VB guys. ;-) ryanm ___ 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 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Re: Newbie AS3 question
Well, I dont agree, this keyword refers to current class, so its only more typing to include it, and making code less readable. Only reason keyword this exists is if you want to pass reference to current object somewhere, in which case you must use this. To me using this in your code makes you look like wannabe-programmer, :) But I gues its matter of taste. btw, old VB does not have this keyword, and if you were reffering to VB(.NET), it is more OOP and more complex than AS3, so I gues programmers in VB.NET (if there are some, since C# is there) are not wannabe-programmers :) ryanm [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What I don't get is why it needs this.addChild instead of just addChild. I've been sick of the keyword this for a long time and have since avoided it in AS2. Any reason that it needs to be back in for AS3? Maybe because it's one of the most useful scope references ever invented? The fundamental concept that you seem to miss is that addChild is meaningless by itself, it is a method of an object (in proper OOP development), and if you just say addChild, who is adding the child? You need a reference. You could do it like this if you like: class Game extends MovieClip { var world:MovieClip; var bg:MovieClip; function Game(){ var GameReference:Game = this; world = new MovieClip(); GameReference.addChild( world ); bg = new MovieClip(); world.addChild( bg ); } } The point is, you shouldn't use functions that aren't attached to objects, it's bad form, and it's thoroughlly confusing to people who later have to maintain your code. Besides, it makes you look like one of those wannabe-programmer VB guys. ;-) ryanm ___ 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] Re: Newbie AS3 question
Passing a reference to the current object is not the only place where using the this prefix is useful. If you come along maintain someone's code 6 months from now and you find a complex method of 200 lines of so, it's useful to have the this prefix to distinguish between variables that are local to the function and those that are available to the instance. Using the this prefix in your code is more likely to make you look like an obsessively meticulious programmer than a newbie in my experience. If the IDE you use automatically highlights the instance variables then there isn't much point in using the this prefix that way, but it's certainly a valid excuse for doing it. Spike On 10/28/05, A.Cicak [EMAIL PROTECTED] wrote: Well, I dont agree, this keyword refers to current class, so its only more typing to include it, and making code less readable. Only reason keyword this exists is if you want to pass reference to current object somewhere, in which case you must use this. To me using this in your code makes you look like wannabe-programmer, :) But I gues its matter of taste. btw, old VB does not have this keyword, and if you were reffering to VB(.NET), it is more OOP and more complex than AS3, so I gues programmers in VB.NET http://VB.NET(if there are some, since C# is there) are not wannabe-programmers :) ryanm [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What I don't get is why it needs this.addChild instead of just addChild. I've been sick of the keyword this for a long time and have since avoided it in AS2. Any reason that it needs to be back in for AS3? Maybe because it's one of the most useful scope references ever invented? The fundamental concept that you seem to miss is that addChild is meaningless by itself, it is a method of an object (in proper OOP development), and if you just say addChild, who is adding the child? You need a reference. You could do it like this if you like: class Game extends MovieClip { var world:MovieClip; var bg:MovieClip; function Game(){ var GameReference:Game = this; world = new MovieClip(); GameReference.addChild( world ); bg = new MovieClip(); world.addChild( bg ); } } The point is, you shouldn't use functions that aren't attached to objects, it's bad form, and it's thoroughlly confusing to people who later have to maintain your code. Besides, it makes you look like one of those wannabe-programmer VB guys. ;-) ryanm ___ 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 -- Stephen Milligan Do you do the Badger? http://www.yellowbadger.com Do you cfeclipse? http://www.cfeclipse.org ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: Newbie AS3 question
Forgot to mention, the other common place you'll see it is in constructors or anywhere else you find yourself with method arguments that match the name of an instance variable. public function Person(fname:String,lname:String) { this.fame = fname; this.lname = lname; } Is it good practice? Probably not. is it commonly seen in code? Definitely. Spike On 10/28/05, Spike [EMAIL PROTECTED] wrote: Passing a reference to the current object is not the only place where using the this prefix is useful. If you come along maintain someone's code 6 months from now and you find a complex method of 200 lines of so, it's useful to have the this prefix to distinguish between variables that are local to the function and those that are available to the instance. Using the this prefix in your code is more likely to make you look like an obsessively meticulious programmer than a newbie in my experience. If the IDE you use automatically highlights the instance variables then there isn't much point in using the this prefix that way, but it's certainly a valid excuse for doing it. Spike On 10/28/05, A.Cicak [EMAIL PROTECTED] wrote: Well, I dont agree, this keyword refers to current class, so its only more typing to include it, and making code less readable. Only reason keyword this exists is if you want to pass reference to current object somewhere, in which case you must use this. To me using this in your code makes you look like wannabe-programmer, :) But I gues its matter of taste. btw, old VB does not have this keyword, and if you were reffering to VB(.NET), it is more OOP and more complex than AS3, so I gues programmers in VB.NEThttp://VB.NET(if there are some, since C# is there) are not wannabe-programmers :) ryanm [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] What I don't get is why it needs this.addChild instead of just addChild. I've been sick of the keyword this for a long time and have since avoided it in AS2. Any reason that it needs to be back in for AS3? Maybe because it's one of the most useful scope references ever invented? The fundamental concept that you seem to miss is that addChild is meaningless by itself, it is a method of an object (in proper OOP development), and if you just say addChild, who is adding the child? You need a reference. You could do it like this if you like: class Game extends MovieClip { var world:MovieClip; var bg:MovieClip; function Game(){ var GameReference:Game = this; world = new MovieClip(); GameReference.addChild( world ); bg = new MovieClip(); world.addChild( bg ); } } The point is, you shouldn't use functions that aren't attached to objects, it's bad form, and it's thoroughlly confusing to people who later have to maintain your code. Besides, it makes you look like one of those wannabe-programmer VB guys. ;-) ryanm ___ 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 -- Stephen Milligan Do you do the Badger? http://www.yellowbadger.com Do you cfeclipse? http://www.cfeclipse.org -- Stephen Milligan Do you do the Badger? http://www.yellowbadger.com Do you cfeclipse? http://www.cfeclipse.org ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Re: Newbie AS3 question
If you come along maintain someone's code 6 months from now and you find a complex method of 200 lines of so, it's useful to have the this prefix to distinguish between variables that are local to the function and those that are available to the instance. true, but i would also immediately re-factor it into shorter, clearer methods. anyway, i think its more a matter of taste rather than good or bad programming. theres places where you need to use it, but otherwise do what you like. :) martin ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders