Search Flex Samples

Override Native Object Methods in Flex 1.5 / Flash 8 /Actionscript 2.0

It is common knowledge that you can extend native classes in Actionscript 2.0 using the prototype inheritance model... Did you know that you can also override native objects and their methods in Flex 1.5 / Flash 8 / Actionscript 2.0 very easily?

This means that you can replace existing functionality with your own logic, or you can extend the functionality of existing objects & methods to suit your custom needs.

Here's a quick example... This example isn't particularly useful, but it exemplifies the concept well.

var old = _global.Array['prototype']['toString'];
_global.Array['prototype']['toString'] = function(){
return "This is the overrided toString method for the Array Object.";
}
_global.Array['prototype']['toString'].old = old;

Now, when you invoke the toString method (as shown below), the result will be from the overrided function.
var test:Array = new Array();
mx.controls.Alert.show(test.toString());

I can hear it now... You are saying "Okay, thats great, so why would you want to do this?". It actually can be particularly useful. Let me give you a couple of scenarios:

  • Override the XML object's sendAndLoad method to append a unique string on the end of the request to prevent caching within the browser/flash player.
    var old = _global.XML['prototype']['sendAndLoad'];
    _global.XML['prototype']['sendAndLoad'] = function(){
    arguments[0] = arguments[0] + "?rand=" + Math.random().toString();
    return arguments.callee.old.apply( this , arguments );
    }
    _global.XML['prototype']['sendAndLoad'].old = old;

  • Override the XML object's sendAndLoad method to preload WSDL doucments within the swf. This reduces the amount of time it takes to initialize web services and reduces the number of callbacks to the web server, although it slightly increases the download size/time for your swf. I personally like this method beause there is less "lag time" in application initialization. This should get you going in the right direction:

    _global.WSDLObjects = {};
    _global.WSDLObjects['/application/services/TestService?WSDL'] = ' ...WSDL DEFINITION GOES HERE ... ';

    var old = _global.XML['prototype']['sendAndLoad'];
    _global.XML['prototype']['sendAndLoad'] = function(){
    var wsdlURL = arguments[0].slice(arguments[0].indexOf("/application"));

    if( _global.WSDLObjects[wsdlURL] != undefined){
    arguments[1].onData( _global.WSDLObjects[wsdlURL] )
    return true;
    }else{
    return arguments.callee.old.apply( this , arguments );
    }
    }
    _global.XML['prototype']['sendAndLoad'].old = old;

I am not sure how well this will work in Flex 2 / Actionscript 3.0. Adobe has made signifigant changes to the prototype inheritance model and now supports class inheritance. I've heard that access to the prototype of native objects is restricted... I'll have to look into this more in the future. In AS3, you can definitely extend a native object class and override the existing functionality.

0 comments:

Related Flex Samples

Learn Flex: Flex Samples | Flex Video Tutorials Flex Examples