View Full Version : Add functionallity to Core Classes
lichorosario
05-15-2007, 11:27 PM
Is there any way to add functionallities to core classes? As Prototype, and Core2 do?
I would like to add the Prototype's addons such as Object.extend(), Enumerable, etc.
I was able to do such things on AS2 modifying intrinsic definitions, but I can't find the way to do this on AS3...
Assertnfailure
05-16-2007, 12:17 AM
The prototype trick was more popular for AS1 as a means to imitating various class-based OOP concepts.
Inheritance would be the preferred approach if you want to extend the logic of a core class in AS2/AS3 (exception for AS2 being stingy classes such as MovieClip).
senocular
05-16-2007, 12:19 AM
The simple answer is no. However, if you wanted... ok, sure, just change the ActionScript dialect in your ActionScript settings (in your publish settings) to ECMAScript and you can go crazy with prototypes.
Array.prototype.getSecondElement = function():*{
if (this.length > 1) {
return this[1];
}
return undefined;
}
var myArray:Array = [1,2,3];
trace(myArray.getSecondElement()); // 2
But why would anyone ever want to do that?
lichorosario
05-16-2007, 04:45 PM
Hi guys.. Thanks for your responses.
Well.. I want to add functionallities to Array and String, for example. I have many utilities to deal with arrays that I really would like to use on AS3. That utilites do things VERY complex, and very usefull.
I don't like the "decorator pattern", neither StringUtilites.trim(myString) ..
I think the cleaner way to do this is to modify the core classes.
I thought there was a way to add methods to AS3 core classes as there was on AS2 (check for the CORE2 project).
I really like the Ruby's style, (and I'm sure I'm not the only one), and the way "www.prototypejs.org" adds new functionalities to core classes.
When I read the ActionScript3 manual, I thought that the new "native" keyword would do something like "intrinisic" did. But there are no Object.as, Array.as files that declares the core classes... So, the way CORE2 project used to do in order to modify those classes I suppose doesn't work anymore.
Right now I'm trying the ECMAScript compiler directive.
In this case... Where should I write the prototype's additions?
I tryied this:
Prototype.as file:
package{
Array.prototype.size = function(){return this.length;}
public class Prototype{
public static function init():void{};
}
}
in my other files:
import Prototype;
Prototype.init(); //this is needed in order to execute the Array.prototype... lines of code.
var a = [1,2,3,4];
trace(a.size());
Is there any better way to do this?
Does anybody knows how CORE2 are dealing with this?
senocular
05-16-2007, 06:14 PM
If you're using Flash, put them in frame 1 of the main timeline. If not using Flash, put them in your document's class constructor (which is also possible with Flash). What you have there also works, but you don't need to make an init method. You can just place the class name in the file and it will be included (and the code in the package body run).
Also, if your class is not in a package you DO NOT need to use import.
lichorosario
05-18-2007, 06:51 PM
Thanks for your response Senocular! This seems to be a great advance... What I don't get to work right now is to add "static" methods, ie: Object.extend (dst:Object,src:Object), String.random(length:int).
Of course, I'm doing this all stuff in order to port AGAIN, the Prototype (prototoypejs.org) libraries to AS3. I did it recently for AS2, including support for XMLHttp, AJAX.Update, and a JSON-RPC library which allows you to call in a transparent way methods from your server scripts.
I hope anybody is doing the same kind of stuff so we can help eachothers.
vinilios
05-19-2007, 02:41 PM
The simple answer is no. However, if you wanted... ok, sure, just change the ActionScript dialect in your ActionScript settings (in your publish settings) to ECMAScript and you can go crazy with prototypes.
Array.prototype.getSecondElement = function():*{
if (this.length > 1) {
return this[1];
}
return undefined;
}
var myArray:Array = [1,2,3];
trace(myArray.getSecondElement()); // 2
But why would anyone ever want to do that?
why not ?
senocular
05-19-2007, 02:48 PM
a) it screws with encapsulation
b) you're basically ditching much of the performance gains you get in AS3 as a result of sealed classes
dr_zeus
05-21-2007, 05:53 PM
a) it screws with encapsulation
b) you're basically ditching much of the performance gains you get in AS3 as a result of sealed classes
Exactly. Creating utility classes (like StringUtil or whatever) is the best way to do it.
praufet
05-22-2007, 10:35 PM
Why not just extend the class and then instantiate your version when you need it?
dr_zeus
05-23-2007, 04:52 PM
praufet, many of the player global classes return primitive types like Arrays or Strings. For instance, if someone subclassed Array to add useful new functions, he or she wouldn't be able to change the return types of functions in the player classes. Creating prototype extensions will work anywhere, but as senocular mentioned, you'll get a performance hit. Util classes seem the best choice (and Adobe built some too, see mx.utils.*) because they're usable anywhere and they don't hurt performance.
lichorosario
05-24-2007, 05:01 PM
I agree with dr_zeus. That's why I'm so interested in extending the core classes. I'm wondering how this issues will be addressed by the Core2 members...
I will miss those beautifull prototype's days... :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.