0

I'm trying to get an array with a randomized set of predefined values.

Suppose I have a regular array of values

var Seed = ['1', true, 3, 'false', 'five']

and I want to pump out variables that take that as seed and return a randomized version of it everytime I

var randomized = new Seed();
console.log(randomized);
// 3, 'five', 1, true, 'false'

or something like that. But everytime...

randomized = new Seed();
console.log(randomized);
// 'five', true, 'false', 1, 3

I even have a nice function to actually randomize the array, but, my array (seed) has to be encapsulated away in a database module along with its randomization code so I could have a clean new ArrayClass() sort of way to do this.

I'm not sure, but it seems to be that if I could either customize an Array Object's constructor function I could plug in that randomization code there along with the seed array and have it pump out a new randomized array each time.

Again, I'm not sure if it's even possible but I've tried a lot of things like

Seed.prototype.Seed = function(){
// cannot set property Seed of undefined
}

Seed.constructor = function(){
// never gets called
}

I thought something like these might've worked but I guess I've no idea what I'm doing. Please elucidate me.

Community
  • 1
  • 1
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
  • 1
    `slice` the array and perform a `Fisher-Yates` shuffle on it, it seems unnecessary to create a `class` to do this, a `function` should work just fine. – Xotic750 Feb 11 '14 at 17:02
  • @Xotic750 I agree it is unnecessary, but just for the sake of whether or not what I was thinking was atleast possible, is it? I.e. possible to either customize an array's contructor function, or making a new array class/object with a custom constructor? – laggingreflex Feb 11 '14 at 17:07
  • I wouldn't modify the constructor of `Array`, but yes you could create a `class` for doing this. – Xotic750 Feb 11 '14 at 17:10
  • So, you're saying you could infact *both* modify the constructor *and* create a class with a custom constructor? (even though you'd rather just do the latter) If so, I'd be greatful if you could post an answer as to how. The randomization bit was really just an my example, I wanted to know how (if) it was possible. – laggingreflex Feb 11 '14 at 17:14

1 Answers1

1

This root of this question seems to be "Can I sub-class Array?"

You can:

function MyArray() {
    this.push("myArray");
}

MyArray.prototype = Object.create(Array.prototype);

var a = new MyArray(); // defaults to ["myArray"]

a instanceof MyArray // true
a instanceof Array // true

As to whether or not this is a good idea in the context of this question, that could be a subject of debate. Depends on the use case.

If all you want is to randomize some values, then I agree with @Xotic750 in the comments. Just create a function to return a regular array.

If you wanted to extend the functionality of the Array class without affecting the native implementation, then sub classing Array is a good idea. Then again, there is a good case for extending the Array prototype directly too:

Array.prototype.myCustomFunction = function() {
    ...
};

var a = [...];
a.myCustomFunction();
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92