I'm using the myarray.length = 0 method to truncate an array via a custom Array.prototype.clear() method.
This has worked so far to my knowledge, but I now have a particular case where it's setting the length to 0, but the elements still remain.
I've run a basic Array test and it seems to work, but in this more complex case, it is not, so it's hard to diagnose the problem.
Here is the extension code on my Array.prototype that I have used...
Object.defineProperty(Array.prototype, 'clear', {
value: function clear() {
// Clear the array using the length = 0 method. See the method's comments on links to more information and benchmarks on this technique.
this.length = 0;
},
enumerable: false,
configurable: true,
writable: false
});
When I call it on all other cases, the following works brilliantly.
var myarray = [ 1,2,3,4,5,6,6,7,8,9, new Date() ];
myarray.clear();
console.log(myarray); // all cleared. { length: 0 }
But in my more complex case where the array has about 10-20 elements and I call the .clear() on the array, it sets the length to 0 but the elements can still be seen if I console.log the array like I did above.
// e.g. { 0: ..., 1: ..., 2: ..., 3: ..., 4: ..., length: 0 }
The only difference is that the Array object I am using is an extended object, with an override, which does call the overridden function. something like so...
function Collection() { Array.apply(this); }
Collection.prototype = Object.create(Array.prototype, {
push: {
enumerable: false,
value: function(item) {
// ...does a type checks on 'item'
Array.prototype.push.apply(this, [ item ]);
}
},
clear: {
enumerable: false,
value: function() { Array.prototype.clear.apply(this); }
}
});
This is happening in Chrome, and I have stepped it thru to the Array.prototype.clear method, just as my testing did, but for some reason it does not work in the second case.
Any ideas?
lengthwould just be another property, and wouldn't change the contents of the object. Note that it's generally a bad idea to extend native prototypesArray- it has "magic powers" compared to other nativesclear(Array)would do the same thing, without touching the prototype. At least it's not enumerable.Array.prototype.splice()until I find something better. @adeneo, this project is massive, so trying to keep things object'ified where possible. Your idea about it being treated as an object; never considered it, as I was calling the Array's prototype function. But interesting to know.