If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Users who have thanked DaveyErwin for this post:

If you use the prototype for attaching methods then you have one copy of the method shared between all the objects that use it. If you don't use prototype then each object will have its own separate copy of the method.

1. A separate copy for each uses more memory.
2. If you have code to change the method then having one copy is easier to change than having an unknown number of separate copies.

If you use the prototype for attaching methods then you have one copy of the method shared between all the objects that use it. If you don't use prototype then each object will have its own separate copy of the method.

1. A separate copy for each uses more memory.
2. If you have code to change the method then having one copy is easier to change than having an unknown number of separate copies.

Because the shuffle method is added to the Array.prototype that single copy of the code can be run for any array including arrays created by that method. If you did not add it to the ptototype then the shuffle method would need a separate copy for each of the arrays instead of sharing the one copy between all arrays.

Because the shuffle method is added to the Array.prototype that single copy of the code can be run for any array including arrays created by that method. If you did not add it to the ptototype then the shuffle method would need a separate copy for each of the arrays instead of sharing the one copy between all arrays.

Hmm. that does not shuffle the shuffled arrays. Simple test/inspection of results reveals.

Users who have thanked Philip M for this post:

you can tack-on prototype methods and properties at any time, but adding own properties would require modifying the constructor code, which might not always be feasible or practical. felgall's array prototype is a good example of this. it's easier to decorate something that already exists and is working, rather than duplicate a bunch of functionality in a "sub class".

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.

Ummm...Philip misspoke. Or perhaps his meaning simply wasn't clear (at least to me). That *does* shuffle the arrays, but it shuffles AND REPLACES the given array.

That is, for example, when you do var d = b.shuffle() you end up shuffling b *IN PLACE* and then assigning that shuffled version to d, resulting in two arrays with the same content. (Or, more correctly, two variables referencing the same array object.)

And because of the use of this.splice(), you lose the original array.

Julien's solution fixes that (or changes that, if it was intended behavior).

Julien's solution fixes that (or changes that, if it was intended behavior).

So my example turned out better than I thought because it illustrates that to fix a problem such as this one that as prototype was used there is only the one copy of the code that needs to be altered to apply the fix.

Ummm...Philip misspoke. Or perhaps his meaning simply wasn't clear (at least to me). That *does* shuffle the arrays, but it shuffles AND REPLACES the given array.

That is, for example, when you do var d = b.shuffle() you end up shuffling b *IN PLACE* and then assigning that shuffled version to d, resulting in two arrays with the same content. (Or, more correctly, two variables referencing the same array object.)

And because of the use of this.splice(), you lose the original array.

Julien's solution fixes that (or changes that, if it was intended behavior).

For the life of me I do not see the use of "two arrays with the same content. (Or, more correctly, two variables referencing the same array object.)".
How can it make sense to lose/replace the original array? That was the point I was trying to make.

FWIIW, I have been involved with Javascript for 15 years and I think that I understand prototypes. But I have never had an occasion to use one or found any situation when one was necessary. I admit that I have never been involved in really large code projects.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.