Somewhere in a prototype extension library, I noticed a shorthand (can''t find it now) for testing whether an parameter is in a set of values. The set was in an object or an array (don''t remember) and resulted in an easy shorthand. Maybe something like: function myFunc (parm1) { if( {2:2, 5:5, 9:9, 17:17}[parm1] ) { // Is parm1 equal to 2, 5, 9 or 17? This seems more verbose that what I recall. What''s a good shorthand technique for testing whether a value is in a set? Sam _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Well you could always use the include function. function myFunc(param1) { if ([2,3,4,5,6].include(param1)) { // is param1 equal to 2, 3, 4, 5 or 6? the include function is a part of Enumerable. On 8/10/06, Sam <sam.google-Uc2IQQBAS6sAvxtiuMwx3w@public.gmane.org> wrote:> > > > Somewhere in a prototype extension library, I noticed a shorthand (can''t > find it now) for testing whether an parameter is in a set of values. > > The set was in an object or an array (don''t remember) and resulted in an > easy shorthand. > > Maybe something like: > > function myFunc (parm1) { > if( {2:2, 5:5, 9:9, 17:17}[parm1] ) { // Is parm1 equal to 2, 5, 9 or 17? > > This seems more verbose that what I recall. > > What''s a good shorthand technique for testing whether a value is in a set? > > Sam > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >
You can also us the member function for arrays: function myFunc(param1) { if ([2,3,4,5,6].member(param1)) { // is param1 a member of the array? -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Brandon Aaron Sent: Sunday, August 13, 2006 8:51 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails-spinoffs] Shorthand for "IN" Well you could always use the include function. function myFunc(param1) { if ([2,3,4,5,6].include(param1)) { // is param1 equal to 2, 3, 4, 5 or 6? the include function is a part of Enumerable. On 8/10/06, Sam <sam.google-Uc2IQQBAS6sAvxtiuMwx3w@public.gmane.org> wrote:> > > > Somewhere in a prototype extension library, I noticed a shorthand > (can''t find it now) for testing whether an parameter is in a set ofvalues.> > The set was in an object or an array (don''t remember) and resulted in > an easy shorthand. > > Maybe something like: > > function myFunc (parm1) { > if( {2:2, 5:5, 9:9, 17:17}[parm1] ) { // Is parm1 equal to 2, 5, 9 or 17? > > This seems more verbose that what I recall. > > What''s a good shorthand technique for testing whether a value is in a set? > > Sam > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Prototype.js can be humbling. Just when I think I''ve got a handle on how to read inside-out, I find something I''ve never seen before and can''t get my head around. I am chasing down a bug in my code which uses Element.classNames, so I wandered off in prototype.js to see how classNames worked. Contest: Explain clearly, with the fewest possible words, how this function works: Element.Methods = { // other methods appear here classNames: function(element) { return new Element.ClassNames(element); },
It looks to me like it creates a new element of that classname. Oh, off-topic a little bit, if you guys are interested, a friend of mine made some tweaks to prototype, check out www.beauscott.com for info. --Will -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Sam Sent: Monday, August 14, 2006 10:55 AM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails-spinoffs] OK. I''m confused again. Prototype.js can be humbling. Just when I think I''ve got a handle on how to read inside-out, I find something I''ve never seen before and can''t get my head around. I am chasing down a bug in my code which uses Element.classNames, so I wandered off in prototype.js to see how classNames worked. Contest: Explain clearly, with the fewest possible words, how this function works: Element.Methods = { // other methods appear here classNames: function(element) { return new Element.ClassNames(element); }, _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs -- Internal Virus Database is out-of-date. Checked by AVG Free Edition. Version: 7.1.394 / Virus Database: 268.10.7/410 - Release Date: 8/5/2006 -- Internal Virus Database is out-of-date. Checked by AVG Free Edition. Version: 7.1.394 / Virus Database: 268.10.7/410 - Release Date: 8/5/2006
Back on topic... It creates a new class/object representing the class names. The class is called Element.ClassNames and it is extended by Enumerable. That means you can iterate through the element''s class names. Pretty cool. On 8/14/06, William Attwood <WAttwood-uwF1JWZk6jFWk0Htik3J/w@public.gmane.org> wrote:> It looks to me like it creates a new element of that classname. Oh, > off-topic a little bit, if you guys are interested, a friend of mine made > some tweaks to prototype, check out www.beauscott.com for info. > > --Will > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Sam > Sent: Monday, August 14, 2006 10:55 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails-spinoffs] OK. I''m confused again. > > > Prototype.js can be humbling. Just when I think I''ve got a handle on how to > read inside-out, I find something I''ve never seen before and can''t get my > head around. > > I am chasing down a bug in my code which uses Element.classNames, so I > wandered off in prototype.js to see how classNames worked. > > Contest: Explain clearly, with the fewest possible words, how this function > works: > > > Element.Methods = { > > // other methods appear here > > classNames: function(element) { > return new Element.ClassNames(element); > }, > > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > -- > Internal Virus Database is out-of-date. > Checked by AVG Free Edition. > Version: 7.1.394 / Virus Database: 268.10.7/410 - Release Date: 8/5/2006 > > > -- > Internal Virus Database is out-of-date. > Checked by AVG Free Edition. > Version: 7.1.394 / Virus Database: 268.10.7/410 - Release Date: 8/5/2006 > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
> It creates a new class/object representing the class names. The class > is called Element.ClassNames and it is extended by Enumerable. That > means you can iterate through the element''s class names. Pretty cool."Extended by Enumerable". Thanks. Now I know where to look. A good answer usually raises other good questions. It seems reasonable to post those questions back here, though I am doing so only to share my thoughts. I hope to dig up the answers myself. 1 - Is every object that is extended by Enumerable going to have iterable class names? 2 - Just how much "baggage" is too much in JavaScript? Prototype is adding *lots* of extra stuff to everything it touches. When does it become a problem? Will memory consumption become an issue, will CPU consumption go through the roof scanning hundreds of methods and properties on every object?
> 1 - Is every object that is extended by Enumerable going to have iterable > class names?Yes but it will probably be necessary to overwrite the _each method so that the Enumerable knows how to iterate through your collection. At least this is my understanding.> 2 - Just how much "baggage" is too much in JavaScript? Prototype is adding > *lots* of extra stuff to everything it touches. When does it become a > problem? Will memory consumption become an issue, will CPU consumption go > through the roof scanning hundreds of methods and properties on every > object?There is a delicate balance between application performance and developer performance. I tend to lean a little more towards developer performance and then address any application perfromances as they become apparent. With that said, I''ve been using prototype on several rather large projects in the last few months and I have yet to see a performance hit worth mentioning. I think in regards ''to adding *lots* of extra stuff'' to your application, you will see the same thing from other libraries. However, the namespace that they put the ''extra stuff'' is different. Typically you are left with typing out verbose namespaces and dealing with less expresive code. Brandon On 8/15/06, Sam <sam.google-Uc2IQQBAS6sAvxtiuMwx3w@public.gmane.org> wrote:> > It creates a new class/object representing the class names. The class > > is called Element.ClassNames and it is extended by Enumerable. That > > means you can iterate through the element''s class names. Pretty cool. > > "Extended by Enumerable". Thanks. Now I know where to look. > > A good answer usually raises other good questions. It seems reasonable to > post those questions back here, though I am doing so only to share my > thoughts. I hope to dig up the answers myself. > > 1 - Is every object that is extended by Enumerable going to have iterable > class names? > > 2 - Just how much "baggage" is too much in JavaScript? Prototype is adding > *lots* of extra stuff to everything it touches. When does it become a > problem? Will memory consumption become an issue, will CPU consumption go > through the roof scanning hundreds of methods and properties on every > object? > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
I couldn''t agree more. And as Thomas himself has expressed here... optimization is the very last thing you need to worry about. On 8/15/06, Brandon Aaron <brandon.aaron-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 1 - Is every object that is extended by Enumerable going to have > iterable > > class names? > > Yes but it will probably be necessary to overwrite the _each method so > that the Enumerable knows how to iterate through your collection. At > least this is my understanding. > > > 2 - Just how much "baggage" is too much in JavaScript? Prototype is > adding > > *lots* of extra stuff to everything it touches. When does it become a > > problem? Will memory consumption become an issue, will CPU consumption > go > > through the roof scanning hundreds of methods and properties on every > > object? > > There is a delicate balance between application performance and > developer performance. I tend to lean a little more towards developer > performance and then address any application perfromances as they > become apparent. > > With that said, I''ve been using prototype on several rather large > projects in the last few months and I have yet to see a performance > hit worth mentioning. > > I think in regards ''to adding *lots* of extra stuff'' to your > application, you will see the same thing from other libraries. > However, the namespace that they put the ''extra stuff'' is different. > Typically you are left with typing out verbose namespaces and dealing > with less expresive code. > > Brandon > > > On 8/15/06, Sam <sam.google-Uc2IQQBAS6sAvxtiuMwx3w@public.gmane.org> wrote: > > > It creates a new class/object representing the class names. The class > > > is called Element.ClassNames and it is extended by Enumerable. That > > > means you can iterate through the element''s class names. Pretty cool. > > > > "Extended by Enumerable". Thanks. Now I know where to look. > > > > A good answer usually raises other good questions. It seems reasonable > to > > post those questions back here, though I am doing so only to share my > > thoughts. I hope to dig up the answers myself. > > > > 1 - Is every object that is extended by Enumerable going to have > iterable > > class names? > > > > 2 - Just how much "baggage" is too much in JavaScript? Prototype is > adding > > *lots* of extra stuff to everything it touches. When does it become a > > problem? Will memory consumption become an issue, will CPU consumption > go > > through the roof scanning hundreds of methods and properties on every > > object? > > > > > > > > > > _______________________________________________ > > Rails-spinoffs mailing list > > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs