Very short snippet: Actionscript extend an array by the method shuffle ()

I had already talked about how to implement the method shuffle () in Javascript and Actionscript . I realized, tuttaavia, not pointing out that it is able to extend Actionscript, Javascript in the same way, its object Array :

1
2
3
prototype . shuffle = function ( ) { Array . prototype. shuffle = function () {
var j : uint , x :*, i : uint = this . length ; i ; j = Math . floor ( Math . random ( ) * i ) , x = this [ -- i ] , this [ i ] = this [ j ] , this [ j ] = x ) ; for (var j: uint , x: *, i: uint = this. length, i, j = Math . floor ( Math . random () * i), x = this [- i], this [i] = this [j], this [j] = x);
}

This code can also be inserted inside a class, for example in the constructor:

1
2
3
4
5
6
7
8
9
package {
Hello extends MovieClip { public class Hello extends MovieClip {
Hello ( ) { public function Hello () {
prototype . shuffle = function ( ) { Array . prototype. shuffle = function () {
var j : uint , x :*, i : uint = this . length ; i ; j = Math . floor ( Math . random ( ) * i ) , x = this [ -- i ] , this [ i ] = this [ j ] , this [ j ] = x ) ; for (var j: uint , x: *, i: uint = this. length, i, j = Math . floor ( Math . random () * i), x = this [- i], this [i] = this [j], this [j] = x);
}
}
}
}

: To then use this "extension" is simply call the meotodo shuffle() from any instance of an array :

1
2
Array = [ "ciao" , "come" , "va" ] ; var a: Array = ["hello", "how", "should"];
; to. shuffle ();

2 comments: "Very short snippet: Actionscript extend an array by the method shuffle ()"

  1. April 27, 2009 Charles :

    Hello, for a while that I read your blog, which is very well maintained and has lots of tips and ideas are not bad. I have to ask you something on this post, one thing that I am not clear what advantage there is in extending a class via the parameter prototype ?
    I typically use a class "MyArray extends Array" , which implements additional methods that I need, and in my projects, the ones. ) (Many thanks to the polymorphism functions that want an object Array see it as an Array )
    Using prototype I think of 'dirty' objects in my design (and apart, which should be included to be more elegant as possible? In the constructor of the Document Class?)

    I salute you and thank you for the info :-)

  2. April 27, 2009 Giovambattista Fazioli :

    @ Charles:

    what advantage there is in extending a class via the parameter prototype?

    Hello Charles, good observation! The use of prototype dates back to the days when it was still an Actionscript object-oriented language "mature" as it is version 3.0. per creare una nuova classe Array personalizzata come hai indicato tu. However, even today, the use of prototype differs from the use of extends to create a new class Array as you have indicated you customized. The advantage, in fact, lies in the possibility of extending indiscriminately all array , without having to specify the type of class. In the method that you used you, which is fine and is accurate, the use of specific methods or properties is bound within your class ( MyArray for instance). With the use of prototype, however, all arrays are extended, even those created as:

    1
    Array = new Array ( ) var test: Array = new Array ()

    instead of writing

    1
    Array = new MyArray ( ) var test: Array = new MyArray ()

    all'uso di classi estese: The advantage of using or not proptotype depends on the situation, but in general there are two reasons for preferring proptotype use of extended classes:
    - proptotype less code to write, just the definition
    - Code already written gets the extension without having to edit
    The second point, in my opinion, is the most interesting. ) su codice già scritto, senza doverlo modificare. It lets you extend the base Actionscriopt objects (such as MovieClip or Array ) of code already written, no need for editing.
    o nel frame 0 a seconda del tipo di progetto. Regarding the inclusion of the code, I use a simple file (type array_ext.as ) that includes the manufacturer of the Document Class in the frame, or 0 depending on the type of project.
    I hope that answers your questions ... if you have additional questions, please contact us.

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL