SPENDLOVE, Matt, FM
2005-Sep-22 09:08 UTC
[Rails-spinoffs] Prototype lib : for-each loops and DontEnum
All I realise that this isn''t strictly the correct place to discuss the prototype lib but being as scriptaculous is dependent on it I thought people might have some opinions. Easier to explain with the example below, copy into a html file and ensure the path to prototype.js is correct. You''ll see the first example prints an extra element "extend" when iterating over the Array using a for-each loop.. - Problem In a nutshell, because prototype adds an extra member variable to the Object type (extend), it kinda breaks the for-each loop (for x in y) for all implicit js objects e.g. Array, presumable because they extend Object. Some code we use (perhaps incorrectly) assumes all members within a custom object are things that it''s interested in. The addition of the "extend" variable breaks things. We use Struts and it''s validator framework falls into this trap. - Solution Well, it''s hacky but we skip members called "extend". Reading the ECMAScript spec and various things on the web (see links) I can see that the language spec sets it''s own internal variables (e.g. length ) with a property called DontEnum which excludes them from a for-each. Unfortunately, there doesn''t seem to be an api to set this yourself :( - Links <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf> - ECMAScript Spec <http://bclary.com/2004/09/26/boot-camp-javascript> - Excellent language overview Any thought / opinions or suggestions welcome. Matt ---------------------------------------------------------------------------- ------ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="imagetoolbar" content="no" /> <title>:::: Test ::::</title> <!-- <link rel="stylesheet" type="text/css" href="style/polymorphic.css" /> --> </head> <body> <script src="javascript/prototype.js" type="text/javascript"></script> <!-- <script src="javascript/effects.js" type="text/javascript"></script> <script src="javascript/dragdrop.js" type="text/javascript"></script> <script src="javascript/controls.js" type="text/javascript"></script> --> <script type="text/javascript"> document.writeln("<br /><br /> Array Test "); var o = new Array("1","2","3"); document.writeln("<br /><br /> test array contents = ["+o+"]"); document.writeln("<br /> c style for loop"); for (var i = 0; i < o.length; i++) { document.writeln("<br /> i["+i+"] = ["+o[i]+"]"); } document.writeln("<br /> for each loop"); for (var j in o) { document.writeln("<br /> j = ["+j+"]"); } document.writeln("<br /><br /> Native Object Test "); var p = new Object(); p.a = "a"; p.b = "b"; p.c = "c"; document.writeln("<br /><br /> test object contents = ["+p+"]"); document.writeln("<br /> c style for loop"); for (var i = 0; i < p.length; i++) { document.writeln("<br /> i["+i+"] = ["+p[i]+"]"); } document.writeln("<br /> for each loop"); for (var j in p) { document.writeln("<br /> j = ["+j+"]"); } document.writeln("<br /><br /> Prototype Object Test "); var TestClass = Class.create(); TestClass.prototype { initialize: function(arg1,arg2,arg3) { this.a = arg1; this.b = arg2; this.c = arg3; }, showMessage: function(msg) { alert(msg); }, forToString: function() { document.writeln("<br /> c style for loop"); for (var i = 0; i < this.length; i++) { document.writeln("<br /> i["+i+"] = ["+this[i]+"]"); } }, forEachToString: function() { document.writeln("<br /> for each loop"); for (var j in this) { document.writeln("<br /> j = ["+j+"]"); } } }; var q = new TestClass("a","b","c"); document.writeln("<br /><br /> test object contents = ["+q+"]"); q.forToString(); q.forEachToString(); </script> </body> </html> ---------------------------------------------------------------------------- ------ *********************************************************************************** The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. Authorised and regulated by the Financial Services Authority This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer. Internet e-mails are not necessarily secure. The Royal Bank of Scotland plc does not accept responsibility for changes made to this message after it was sent. Whilst all reasonable care has been taken to avoid the transmission of viruses, it is the responsibility of the recipient to ensure that the onward transmission, opening or use of this message and any attachments will not adversely affect its systems or data. No responsibility is accepted by The Royal Bank of Scotland plc in this regard and the recipient should carry out such virus and other checks as it considers appropriate. Visit our websites at: http://www.rbs.co.uk/CBFM http://www.rbsmarkets.com ********************************************************************************
Thomas Fuchs
2005-Sep-22 09:20 UTC
[Rails-spinoffs] Prototype lib : for-each loops and DontEnum
This is fixed in the Prototype 1.4.0 pre-releases. Thomas Am 22.09.2005 um 14:45 schrieb SPENDLOVE, Matt, FM:> All > > I realise that this isn''t strictly the correct place to discuss the > prototype lib but being as scriptaculous is dependent on it I > thought people > might have some opinions. > > Easier to explain with the example below, copy into a html file and > ensure > the path to prototype.js is correct. You''ll see the first example > prints an > extra element "extend" when iterating over the Array using a for- > each loop.. > > - Problem > > In a nutshell, because prototype adds an extra member variable to > the Object > type (extend), it kinda breaks the for-each loop (for x in y) for all > implicit js objects e.g. Array, presumable because they extend Object. > > Some code we use (perhaps incorrectly) assumes all members within a > custom > object are things that it''s interested in. The addition of the > "extend" > variable breaks things. > > We use Struts and it''s validator framework falls into this trap.
Rob Wills
2005-Sep-22 09:26 UTC
[Rails-spinoffs] Prototype lib : for-each loops and DontEnum
On 22/09/05, SPENDLOVE, Matt, FM <Matt.SPENDLOVE@rbos.com> wrote:> > Easier to explain with the example below, copy into a html file and ensure > the path to prototype.js is correct. You''ll see the first example prints an > extra element "extend" when iterating over the Array using a for-each loop.. >> > Any thought / opinions or suggestions welcome. >Matt, what version of prototype are you using? I had the exact same problem with the additional ''extend'' attribute, until prototype was upgraded recently (in the last few months). You might notice the section commented out at line 35 of prototype.js version 1.4.0_pre2 ... /* Object.prototype.extend = function(object) { return Object.extend.apply(this, [this, object]); } */ Cheers, Rob
SPENDLOVE, Matt, FM
2005-Sep-22 10:35 UTC
[Rails-spinoffs] Prototype lib : for-each loops and DontEnum
Scriptaculous 1.0 Prototype 1.3.1 I guess that explains things. Not sure I have time to integrate / test a pre release version though before my next app release :( Thanks to all Matt> -----Original Message----- > From: rails-spinoffs-bounces@lists.rubyonrails.org > [mailto:rails-spinoffs-bounces@lists.rubyonrails.org] On > Behalf Of Rob Wills > Sent: 22 September 2005 14:04 > To: rails-spinoffs@lists.rubyonrails.org > Subject: Re: [Rails-spinoffs] Prototype lib : for-each loops > and DontEnum > > > On 22/09/05, SPENDLOVE, Matt, FM <Matt.SPENDLOVE@rbos.com> wrote: > > > > Easier to explain with the example below, copy into a html file and > > ensure the path to prototype.js is correct. You''ll see the first > > example prints an extra element "extend" when iterating > over the Array > > using a for-each loop.. > > > > > > > > Any thought / opinions or suggestions welcome. > > > > Matt, what version of prototype are you using? > > I had the exact same problem with the additional ''extend'' > attribute, until prototype was upgraded recently (in the last > few months). > > You might notice the section commented out at line 35 of > prototype.js version 1.4.0_pre2 ... > > /* > Object.prototype.extend = function(object) { > return Object.extend.apply(this, [this, object]); > } > */ > > Cheers, > > Rob > _______________________________________________ > Rails-spinoffs mailing list Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >*********************************************************************************** The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. Authorised and regulated by the Financial Services Authority This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer. Internet e-mails are not necessarily secure. The Royal Bank of Scotland plc does not accept responsibility for changes made to this message after it was sent. Whilst all reasonable care has been taken to avoid the transmission of viruses, it is the responsibility of the recipient to ensure that the onward transmission, opening or use of this message and any attachments will not adversely affect its systems or data. No responsibility is accepted by The Royal Bank of Scotland plc in this regard and the recipient should carry out such virus and other checks as it considers appropriate. Visit our websites at: http://www.rbs.co.uk/CBFM http://www.rbsmarkets.com ********************************************************************************
Jon Tirsen
2005-Sep-22 18:15 UTC
[Rails-spinoffs] Prototype lib : for-each loops and DontEnum
Just out of interest. What is it replaced with? (Yeah, I''m too lazy to read the code...) Cheers, Jon On 9/22/05, Thomas Fuchs <thomas@fesch.at> wrote:> This is fixed in the Prototype 1.4.0 pre-releases. > > Thomas > > Am 22.09.2005 um 14:45 schrieb SPENDLOVE, Matt, FM: > > > All > > > > I realise that this isn''t strictly the correct place to discuss the > > prototype lib but being as scriptaculous is dependent on it I > > thought people > > might have some opinions. > > > > Easier to explain with the example below, copy into a html file and > > ensure > > the path to prototype.js is correct. You''ll see the first example > > prints an > > extra element "extend" when iterating over the Array using a for- > > each loop.. > > > > - Problem > > > > In a nutshell, because prototype adds an extra member variable to > > the Object > > type (extend), it kinda breaks the for-each loop (for x in y) for all > > implicit js objects e.g. Array, presumable because they extend Object. > > > > Some code we use (perhaps incorrectly) assumes all members within a > > custom > > object are things that it''s interested in. The addition of the > > "extend" > > variable breaks things. > > > > We use Struts and it''s validator framework falls into this trap. > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
Michael Schuerig
2005-Sep-22 21:24 UTC
[Rails-spinoffs] Re: Prototype lib : for-each loops and DontEnum
On Thursday 22 September 2005 23:51, Jon Tirsen wrote:> Just out of interest. What is it replaced with? (Yeah, I''m too lazy > to read the code...)something.extend(somethingElse) is replaced by Object.extend(something, somethingElse) Michael -- Michael Schuerig Those people who smile a lot mailto:michael@schuerig.de Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You