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 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 _______________________________________________ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders