Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-28-2006, 02:12 AM   #1
Mathieu_BSL
Senior Member
 
Join Date: Oct 2001
Location: Fiji
Posts: 187
Default Can we extend dynamically a class ?

Well, I just installed Flex 2 and I've been playing with
ActionScript 3.0 for a couple of hours.

I have 2 little questions :

1) Is it possible to extend dynamically a class or an object ?
For example, in ActionScript 2.0, we can do this :

var a = {};
var b = {};
a.__proto__ = b;

And we can do this as well :

_global.com.Circle.prototype.__proto__ == _global.com.Shape.prototype;

Is there any equivalent in Flex 2 ?

2) Is it possible to compile a SWF without including all
mx classes each time so that we can optimize file sizes ?

I am a little bit sad...
Mathieu_BSL is offline   Reply With Quote
Old 07-28-2006, 12:04 PM   #2
Fall_X
Registered User
 
Join Date: Jul 2006
Posts: 85
Default

I'm not sure, but I think that if you declare a class as dynamic, you can add properties to an instance of that class. As for using prototype, I don't know, but why use something like "_global.com.Circle.prototype.__proto__ == _global.com.Shape.prototype;" when you can use proper inheritance? __proto__ is AS1 code, sure it works in AS2 but it's just an old, ugly coding style.

Quote:
2) Is it possible to compile a SWF without including all
mx classes each time so that we can optimize file sizes ?
If I understand it correctly, compilation is already optimized. For instance, it is safe to use * when importing a package, because only the classes and functions you are actually using are imported. I'm guessing that's the same with the mx classes. However, I use Flash 9 and not Flex, so I really don't know much about this.
Fall_X is offline   Reply With Quote
Old 07-28-2006, 02:24 PM   #3
diogo
Member
 
diogo's Avatar
 
Join Date: Sep 2005
Location: Brasil
Posts: 59
Default

just to complement what Fall_X said, a quote from prog_actionscript30.pdf:

Quote:
In the past, advanced ActionScript programmers could directly manipulate the prototype
chain with special built-in language elements. Now that the language provides a more mature
implementation of a class-based programming interface, many of these special language
elements, such as __proto__ and __resolve, are no longer part of the language. Moreover,
optimizations of the internal inheritance mechanism that provide significant Flash Player
performance improvements preclude direct access to the inheritance mechanism.
so use the as3's inheritance mechanism
diogo is offline   Reply With Quote
Old 07-28-2006, 02:44 PM   #4
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

Class usage always follows the rules of use what is needed, but in Flex, you have a framework which may or may not be designed to take optimal advantage of that (and in my experience I feel that Flex does not). If you're not after RIAs, and you're looking for optimization, Flex is not for you. You'd be better off using Flash 9 or the Flex SDK compiler without the flex mx framework to do what you need to do.
__________________
(6)
senocular is offline   Reply With Quote
Old 07-28-2006, 07:53 PM   #5
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

__proto is no longer there, but the prototype chains still exist (alongside the inheritance mechanism in what can pass as a triumph of language development bastardization which might well be the future ;D), and you can modify them dynamically. If you are compiling in strict mode, you have to declare you class dynamic to be able to mod its prototype.
Example:

testPackage/TestClass.as
ActionScript Code:
package testPackage {     public dynamic class TestClass {             public function TestClass() {}         public function builtInMethod() {         trace("Built-In Method called.");     }     } }

fla code
ActionScript Code:
import testPackage.TestClass; var t:TestClass = new TestClass(); t.builtInMethod(); // "built-in method called" TestClass.prototype.dynamicMethod = function () { trace("dynamic method called.");} t.dynamicMethod(); // "dynamic method called"

Oh yeah, and _global is no longer there.
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).
astgtciv is offline   Reply With Quote
Old 07-29-2006, 01:52 PM   #6
Mathieu_BSL
Senior Member
 
Join Date: Oct 2001
Location: Fiji
Posts: 187
Default

Thank you all for your comments.

Fall X : I know about the * logic, but that's not really the problem.
In fact, what I consider as a "project" is a collection of swf
files, and each swf file contain a collection of classes.
I use a special class to build the inhritance between all my classes
at run time. So, a class is NEVER in 2 differents swf files,
there is no repetition. That's something very important for me.

With Flex, each swf include all the classes which are needed.
I had a look to one of the sample on Adobe site, and they effectively
load the same collection of classes each time they want to load
a new SWF in their main SWF. Thus, you load many "200 ko" each time
you want to include a new interface, I think that's simply ridiculous...

Diogo : So I understand that there will never be any way in the future
to avoid duplication of classes in complex projects ? It's terrible !

astgtciv : I know about the dynamic keyword, but that's not really what
I need in fact. In your example, you add a method, but you
do not modify the inheritance... Maybe there is an hidden keyword for
that ?...

By the way, I consider that the _global keyword is cool, as I store all
my classes in a "_global.com.namespace.Classes" structure, and in order
to avoid the inclusion of classes during the compilation, I use that
syntax : _global.com.myNamespace.MyClass
Mathieu_BSL is offline   Reply With Quote
Old 07-29-2006, 02:35 PM   #7
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

I thought I would add that you can "dynamically extend classes" at runtime, but you cannot change the class association of isntances at runtime - which is what I think you're really after. So once you create an instance, you can't change its class association. You would need __proto__ for that. You can, however, change a class's superclass simply by redefining its prototype - and this, by the way is not for class classes defined with the class keyword (they are locked), but rather constructor function classes defined in the old AS1 style.
__________________
(6)
senocular is offline   Reply With Quote
Old 07-30-2006, 01:38 PM   #8
Mathieu_BSL
Senior Member
 
Join Date: Oct 2001
Location: Fiji
Posts: 187
Default

senocular :

What I want to do is only to build dynamically
the inheritance chain between classes at run time
instead of a compilation time (it's the only way
to proceed if we want to avoid duplication of
classes in many swfs), and then I can instanciate
my objects.

So, in fact, once my "function" objects are
instanciated, they always keep their class.

apple = new apple ();

The "apple" class proto chain will never change.

As you can understand, I must build the inheritance
chain before any instanciation.

So, what I would like to do is something like this
in ASV2 :

apple.prototype.__proto__ = fruit.prototype;

As you can see, the apple class keep it's methods
and properties and will inherite of the fruit methods
and properties.

Can you give me a small example of that in AS3 please,
I am not sure to understand what you say.

Mathieu
Mathieu_BSL is offline   Reply With Quote
Old 07-30-2006, 04:38 PM   #9
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

As has been said, its not possible in AS3
__________________
(6)
senocular is offline   Reply With Quote
Old 07-30-2006, 05:37 PM   #10
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

Could you give an example of prototype inheritance in AS3, senocular? I can't seem to get it to work... AS3Langref says:

Quote:
A class's prototype object is a special instance of that class that provides a mechanism for sharing state across all instances of a class. At run time, when a property is not found on a class instance, the delegate, which is the class prototype object, is checked for that property. If the prototype object does not contain the property, the process continues with the prototype object's delegate checking in consecutively higher levels in the hierarchy until Flash Player finds the property.
I tried it with both a function-style class and a "normal" class, and neither seems to work...
ActionScript Code:
import testPackage.*; // Will try using tf as an ancestor prototype var tf:Function = function(){}; tf.prototype.test = function() {trace("test");} var AS1StyleClass = function(){}; AS1StyleClass.prototype.prototype = tf.prototype; var t:* = new AS1StyleClass(); trace(t.test); //undefined // TestClass is a class in testPackage TestClass.prototype.prototype = tf.prototype; var tc:TestClass = new TestClass(); trace(tc.test); // undefined

Is the "prototype object's delegate" not simply the prototype of the prototype object's?
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).

Last edited by astgtciv; 07-30-2006 at 05:40 PM.
astgtciv is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the mx.util.Delegate class within your classes! madgett ActionScript 2.0 3 09-02-2007 11:11 PM
Database simulated with arrays on a cd-rom lecasn5 Components 61 09-07-2004 11:40 AM
How to link a class to dynamically created movie clip. vijayd ActionScript 2.0 13 01-25-2004 09:16 PM
card game: designing Deck class and good OOP? pigpen Gaming and Game Development 1 11-26-2002 11:41 AM


All times are GMT. The time now is 01:21 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.