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 :










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 objectArraysee it as anArray)Using
prototypeI 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
@ Charles:
Hello Charles, good observation! The use of
prototypedates back to the days when it was still an Actionscript object-oriented language "mature" as it is version 3.0. per creare una nuova classeArraypersonalizzata come hai indicato tu. However, even today, the use ofprototypediffers from the use ofextendsto create a new classArrayas you have indicated you customized. The advantage, in fact, lies in the possibility of extending indiscriminately allarray, 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 (MyArrayfor instance). With the use of prototype, however, all arrays are extended, even those created as:instead of writing
all'uso di classi estese: The advantage of using or not
proptotypedepends on the situation, but in general there are two reasons for preferringproptotypeuse of extended classes:-
proptotypeless 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
MovieCliporArray) 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 theDocument Classin the frame, or 0 depending on the type of project.I hope that answers your questions ... if you have additional questions, please contact us.