Daanoz wrote:
> today i was working some javascript when my array's didn't behave as
> expected, so after some research i found prototype was the cause...
> try my test to see what i mean, so my question is, how can i loop
> associative array (so i can get all keys for the array) without
> prototype giving me all functions?
Javascript does not have associative arrays, it has objects that have
name/value pairs. An Array is an Object with a special length
property and some extra methods (join, shift, pop, etc.). You should
not use an Array where a plain Object is needed.
[...]
> <title>Test HTML</title>
> <script src="prototype.js" type="text/javascript"></script>
> <script type="text/javascript">
>
> function CTest()
> {
> this.aTest = new Array();
You can initialise an array with:
this.aTest = [];
> this.aTest["key1"] = "val1";
That adds a property called 'key1' with a value of 'val1', however
since the property name is not a positive integer, it is not treated
as an array element: the length of the array is still zero.
> }
>
> CTest.prototype.printArray = function()
> {
> var PrintOut = "<table>";
>
> for(x in this.aTest) {
You should not let x become global unless you really mean to do that.
You will iterate over all the properties of the array, including the
many that Prototoype.js adds. Since javascript does not have a
mechanism for adding DONTENUM properties, you can test each property
with hasOwnProperty:
var t = this.aTest;
for (var p in t) {
if (t.hasOwnProperty(p)) {
...
support is lacking in older versions of IE. The best solution is to
use an Object, not an Array. Prototype.js used to add properties to
Object too, but doesn't do that since version 1.5.
> PrintOut += "<tr><td>" + x;
IE is notoriously slow with the += compound operator, try using an
array instead:
var PrintOut = [];
for (var p in t) {
if (t.hasOwnProperty(p)) {
PrintOut.push("<tr><td>" + p);
}
}
PrintOut.push('</table>');
document.getElementById('result').innerHTML = PrintOut.join('');
...
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---