[Flashcoders] Adding a function to the String prototype
I thought this was as easy as: String.prototype.traceFoo = function():Void { trace(foo); } var s:String = new String(); s.traceFoo(); But that results in an error, There is no method with the name 'traceFoo'. Well, duh, traceFoo doesn't exist until the code executes, but how do you tell the compiler that? How does one add a function to String? P. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Adding a function to the String prototype
Modifying native object prototypes is a hack. Try extending the String class instead: class myPackage.MyString extends String { public function traceFoo():Void { trace(foo); } } // ... import myPackage.MyString; var s:MyString = new MyString(); s.traceFoo(); If you absolutely must use it the way you describe, you can do this to sidestep compiler errors: s[traceFoo](); (Now forget I ever said that.) On 2/13/07, Pete Miller [EMAIL PROTECTED] wrote: I thought this was as easy as: String.prototype.traceFoo = function():Void { trace(foo); } var s:String = new String(); s.traceFoo(); But that results in an error, There is no method with the name 'traceFoo'. Well, duh, traceFoo doesn't exist until the code executes, but how do you tell the compiler that? How does one add a function to String? P. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com -- T. Michael Keesey Director of Technology Exopolis, Inc. 2894 Rowena Avenue Ste. B Los Angeles, California 90039 -- The Dinosauricon: http://dino.lm.com Parry Carney: http://parryandcarney.com ISPN Forum: http://www.phylonames.org/forum/ ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Adding a function to the String prototype
Hello :) If you use the strict typing with var s:String you use the AS2 class interpretor and you must override the String.as file in the en/firstRun/classes/FP8 directory in your flash install path. The intrinsic of the String core class must be changed by default in AS2. The String class isn't dynamic ! For me if you want overrides your Core Objects in think you can use Core2 : http://code.google.com/p/core2/ This library implement all ECMAScript methods in the core class (Array, String, etc...). In the AS2 version you can use the custom intrinsic class of Core2 for example to compare the Adobe class and the Core2 class. eKA+ :) 2007/2/13, Pete Miller [EMAIL PROTECTED]: I thought this was as easy as: String.prototype.traceFoo = function():Void { trace(foo); } var s:String = new String(); s.traceFoo(); But that results in an error, There is no method with the name 'traceFoo'. Well, duh, traceFoo doesn't exist until the code executes, but how do you tell the compiler that? How does one add a function to String? P. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] Adding a function to the String prototype
I see, thanx to all. P. -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of eka Sent: Tuesday, February 13, 2007 2:21 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] Adding a function to the String prototype Hello :) If you use the strict typing with var s:String you use the AS2 class interpretor and you must override the String.as file in the en/firstRun/classes/FP8 directory in your flash install path. The intrinsic of the String core class must be changed by default in AS2. The String class isn't dynamic ! For me if you want overrides your Core Objects in think you can use Core2 : http://code.google.com/p/core2/ This library implement all ECMAScript methods in the core class (Array, String, etc...). In the AS2 version you can use the custom intrinsic class of Core2 for example to compare the Adobe class and the Core2 class. eKA+ :) 2007/2/13, Pete Miller [EMAIL PROTECTED]: I thought this was as easy as: String.prototype.traceFoo = function():Void { trace(foo); } var s:String = new String(); s.traceFoo(); But that results in an error, There is no method with the name 'traceFoo'. Well, duh, traceFoo doesn't exist until the code executes, but how do you tell the compiler that? How does one add a function to String? P. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] Adding a function to the String prototype
You should absolutely feel comfortable and fine using prototype to add functions to the String native object if you want. I say if it works, use it. Just because they skimped on String and Array methods that MOST other languages have doesn't mean you should have to write a bunch of extra code to extend the class or cast your Strings as MyStrings. Feel free to use prototype until you move to AS3. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com