I want to make a JavaScript class vector
which is a zero-initialized array. I'll probably want to add math functionality later, but I don't want to sacrifice the memory or performance qualities of the native types because the program operates on a lot of data. (It's basically scientific visualization.)
To insert Array.prototype
in the prototype chain, I tried to use
vector.prototype = Object.create( Array.prototype );
Firefox gives me an error
TypeError: Array.prototype.toSource called on incompatible Object
Is this a bug in Firefox? It seems to work in Webkit.
So, I tried to use Float32Array
which is closer to what I want, and zero-initialized by default anyway.
var vector = function( size ) { Float32Array.call( this, size ); }
vector.prototype = Object.create( Float32Array.prototype );
In Firefox, this runs but new
doesn't initialize objects properly. In Webkit new vector
throws an exception. Makes no difference if I use vector.prototype = Float32Array.prototype
instead.
Am I fundamentally asking for too much?