Daanoz
2007-Dec-27 13:32 UTC
Array prototype looping through functions instead of variables!
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? Test HTML: ************************************************************************************* <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl"> <head> <title>Test HTML</title> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="prototype.js" type="text/javascript"></script> <!-- Prototype JavaScript framework, version 1.6.0 --> <script type="text/javascript"> function CTest() { this.aTest = new Array(); this.aTest["key1"] = "val1"; } CTest.prototype.printArray = function() { var PrintOut = "<table>"; for(x in this.aTest) { PrintOut += "<tr><td>" + x; PrintOut += "</td><td>"; PrintOut += this.aTest[x]; PrintOut += "</td></tr>"; } PrintOut += "</table>"; document.getElementById(''result'').innerHTML = PrintOut; } var Test = new CTest(); </script> </head> <body> <div onclick="Test.printArray()">clickme</div> <div id="result"></div> </body> </html> --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Kaspick
2007-Dec-27 13:35 UTC
Re: Array prototype looping through functions instead of variables!
Simple answer, don''t use an array as an "associative array"; use an Object. Arrays aren''t supposed to be used as you''re using them. It''s not a prototype issue. If you want the details, do a group search, you''ll find many posts on the subject. On 12/27/07, Daanoz <Daanpda-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Test HTML: > > ************************************************************************************* > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// > www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl"> > <head> > <title>Test HTML</title> > <meta name="description" content="" /> > <meta name="keywords" content="" /> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> > <script src="prototype.js" type="text/javascript"></script> <!-- > Prototype JavaScript framework, version 1.6.0 --> > <script type="text/javascript"> > > function CTest() > { > this.aTest = new Array(); > this.aTest["key1"] = "val1"; > } > > CTest.prototype.printArray = function() > { > var PrintOut = "<table>"; > > for(x in this.aTest) { > PrintOut += "<tr><td>" + x; > PrintOut += "</td><td>"; > PrintOut += this.aTest[x]; > PrintOut += "</td></tr>"; > } > > PrintOut += "</table>"; > document.getElementById(''result'').innerHTML = PrintOut; > } > > var Test = new CTest(); > </script> > </head> > <body> > <div onclick="Test.printArray()">clickme</div> > <div id="result"></div> > </body> > </html> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Quadling
2007-Dec-27 13:42 UTC
Re: Array prototype looping through functions instead of variables!
On 27/12/2007, Daanoz <Daanpda-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Test HTML: > > ************************************************************************************* > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// > www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl"> > <head> > <title>Test HTML</title> > <meta name="description" content="" /> > <meta name="keywords" content="" /> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> > <script src="prototype.js" type="text/javascript"></script> <!-- > Prototype JavaScript framework, version 1.6.0 --> > <script type="text/javascript"> > > function CTest() > { > this.aTest = new Array();Replace this line ...> this.aTest["key1"] = "val1";with this line ... this.aTest = { "key1" : "val1"};> } > > CTest.prototype.printArray = function() > { > var PrintOut = "<table>"; > > for(x in this.aTest) { > PrintOut += "<tr><td>" + x; > PrintOut += "</td><td>"; > PrintOut += this.aTest[x]; > PrintOut += "</td></tr>"; > } > > PrintOut += "</table>"; > document.getElementById(''result'').innerHTML = PrintOut; > } > > var Test = new CTest(); > </script> > </head> > <body> > <div onclick="Test.printArray()">clickme</div> > <div id="result"></div> > </body> > </html> > > > >-- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Quadling
2007-Dec-27 13:43 UTC
Re: Array prototype looping through functions instead of variables!
On 27/12/2007, Daanoz <Daanpda-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Test HTML: > > ************************************************************************************* > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// > www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl"> > <head> > <title>Test HTML</title> > <meta name="description" content="" /> > <meta name="keywords" content="" /> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> > <script src="prototype.js" type="text/javascript"></script> <!-- > Prototype JavaScript framework, version 1.6.0 --> > <script type="text/javascript"> > > function CTest() > { > this.aTest = new Array(); > this.aTest["key1"] = "val1"; > } > > CTest.prototype.printArray = function() > { > var PrintOut = "<table>"; > > for(x in this.aTest) { > PrintOut += "<tr><td>" + x; > PrintOut += "</td><td>"; > PrintOut += this.aTest[x]; > PrintOut += "</td></tr>"; > } > > PrintOut += "</table>"; > document.getElementById(''result'').innerHTML = PrintOut; > } > > var Test = new CTest(); > </script> > </head> > <body> > <div onclick="Test.printArray()">clickme</div> > <div id="result"></div> > </body> > </html> > > > >Replace this line ... this.aTest["key1"] = "val1"; with this line ... this.aTest = { "key1" : "val1"}; -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
RobG
2007-Dec-27 23:12 UTC
Re: Array prototype looping through functions instead of variables!
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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---