HI there, I''m trying to improve the performance of an application we''re working on which makes a fair bit of use of the eval statement. We''re tryignt o weed out the use of eval due to the performacne overhead and there is one section of code we haven''t as yet been able to improve upon. Any help very much appreciated. Here is a sample code snipped. The key points which ''cannot'' be changed is that the classname must be sourced from a variable and not static (same for the parameters). var widgetName = "Button"; var widgetParam1 = "I am parameter 1"; var widgetParam2 = new Array("I am ", "parameter 2"); eval("var myWidget = new "+widgetName+"(widgetParam1, widgetParam2);"); Many thanks Gareth --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
GarethAtFlignet wrote:> Here is a sample code snipped. The key points which ''cannot'' be > changed is that the classname must be sourced from a variable and not > static (same for the parameters). > > var widgetName = "Button"; > var widgetParam1 = "I am parameter 1"; > var widgetParam2 = new Array("I am ", "parameter 2"); > eval("var myWidget = new "+widgetName+"(widgetParam1, > widgetParam2);");I suppose you could do something like this: var widget1 = new Object(); // Wherever the widgets come from... var widget2 = new Object(); [...] var Widgets = new Object(); Widgets[someWidgetname] = widget1; Widgets[anotherWidgetname] = widget2; [...] var widgetName = "Button"; var widgetParam1 = "I am parameter 1"; var widgetParam2 = new Array("I am ", "parameter 2"); var myWidget = new Widgets[widgetName](widgetParam1, widgetParam2); (I''ve never tried anything like that, but I suppose it must be possible...) -- Bertilo Wennergren <http://bertilow.com> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Could you use ... var myWidget = document.createElement(widgetName); and then set the properties as normal? On 16/01/2008, GarethAtFlignet <garethinwales-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > HI there, > > I''m trying to improve the performance of an application we''re working > on which makes a fair bit of use of the eval statement. We''re tryignt > o weed out the use of eval due to the performacne overhead and there > is one section of code we haven''t as yet been able to improve upon. > Any help very much appreciated. > > Here is a sample code snipped. The key points which ''cannot'' be > changed is that the classname must be sourced from a variable and not > static (same for the parameters). > > var widgetName = "Button"; > var widgetParam1 = "I am parameter 1"; > var widgetParam2 = new Array("I am ", "parameter 2"); > eval("var myWidget = new "+widgetName+"(widgetParam1, > widgetParam2);"); > > Many thanks > Gareth > > >-- ----- 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 -~----------~----~----~----~------~----~------~--~---
Sorry Richard I should have put in the Button object for you to look at as well, its not a html element but a prototype class: <script> var AbstractWidget = Class.create({ widgetParam1: null, widgetParam2: null, initialize: function(widgetParam1, widgetParam2) { this.widgetParam1 = widgetParam1; this.widgetParam2 = widgetParam2; }, }); var AbstractComponent = Class.create(AbstractWidget, { } ); var Button = Class.create(AbstractComponent, { } ); </script> On Jan 16, 10:27 am, "Richard Quadling" <rquadl...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Could you use ... > > var myWidget = document.createElement(widgetName); > > and then set the properties as normal? > > On 16/01/2008, GarethAtFlignet <garethinwa...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > > > HI there, > > > I''m trying to improve the performance of an application we''re working > > on which makes a fair bit of use of the eval statement. We''re tryignt > > o weed out the use of eval due to the performacne overhead and there > > is one section of code we haven''t as yet been able to improve upon. > > Any help very much appreciated. > > > Here is a sample code snipped. The key points which ''cannot'' be > > changed is that the classname must be sourced from a variable and not > > static (same for the parameters). > > > var widgetName = "Button"; > > var widgetParam1 = "I am parameter 1"; > > var widgetParam2 = new Array("I am ", "parameter 2"); > > eval("var myWidget = new "+widgetName+"(widgetParam1, > > widgetParam2);"); > > > Many thanks > > Gareth > > -- > ----- > 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 -~----------~----~----~----~------~----~------~--~---
Try: var myWidget = new window[widgetName](widgetParam1, widgetParam2); best, Tobie --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Many Thanks all. Tobie, didn''t think of using window (although silly me I do actually test for window.widgetName beorehand to check if the class exists), many many thanks. Cheers Gareth On Jan 16, 10:50 am, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try: > > var myWidget = new window[widgetName](widgetParam1, widgetParam2); > > best, > > Tobie--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 16/01/2008, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Try: > > var myWidget = new window[widgetName](widgetParam1, widgetParam2); > > best, > > TobieOh. Nice. Using the namespace of window to find an entity named by a variable. Nice and abstract. As I always say, we are all standing on the shoulders of some very clever giants. -- ----- 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 -~----------~----~----~----~------~----~------~--~---