Does anyone know why a new Array Contains data when just created when a script manager is running on the page... I do the following code:
<script language="javascript">
var myArray = new Array();
for (x in myArray)
{
document.write(myArray[x])
}
</script>
And get the output: function (index) { this.splice(index, 1); }
WHY!?!?!??!!?
Hi,
this happens because Array is extended through the prototype object, i.e.
Array.prototype.splice = ...
The solution is to extend Array using "type" methods:
Array.splice = ...
I believe this will be fixed in the next release.
hello.
As always, Garbin is correct. btw, i'd just like to add that, in my opinion, the for each usage in arrays which expects to get only the elements is just a side effect of the way foreach works (in other words, i think that even though everyone uses that for getting the elements of an array, it's not what it was meant for)
Actually you are absolutely right. I have solved the problem and what i did was just remove the:
for (x in Array)
and replace it with:
for (x = 0; x < Array.length; x++)
this seemed to fix it right up. It wasn't the fact that the array actually had those values in it. It's just somehow they array accessed those things when used in the for each statement... As gabin said it has to do with prototyping, which i was not familiar with. Thanks for the help guys, and the explanation.
hello again.
btw, i do believe that if you go the other way around, you'll have amore performant solution (ie, instead of goin from 0 to length, do it the other way around - if you can, of course).
I can't. It seems to always be populated with that data otherwise... I don't see why it would cause better performance. My array lengths are exact and not allocated longer then there last bit of information.
hello.
well, it all dependes on the number of items. check this and let me know:
http://www.devpro.it/examples/loopsbench/
No comments:
Post a Comment