Re: Extension for writing concise, expressive code

kangax
Fri, 21 Sep 2007 23:35:10 -0000

pre{background:#eef;padding:5px}
Ha, enumerable on steroids!

Ok, CSS3 selectors partially accomplish this so instead of:

$$(''.register input'').findAll(function(el){return el.checked})


we do


$$(''.register input:checked'')


Selectors, of course, are quite limited and are not fully supported as of
now (while :empty, :enabled and :disabled work, :contains doesn''t).
Besides,
thinking about all the prototype''s Element.Methods which would be
possible
to use, this looks pretty sweet.


Your example:


[''FOO'', ''BAR'',
''BAZ''].map(''toLowerCase.toArray'')


can now be done easily with invoke (though a little more verbose):


[''FOO'', ''BAR'',
''BAZ''].invoke(''toLowerCase'').invoke(''toArray'');


As far as syntax, I think parameters should not be passed as an array:


someDivs.findAll([''hasClassName'',
''myClass'']);


following invoke''s philosophy it should be simply:


someDivs.findAll(''hasClassName'', ''myClass'');
// just like
someDivs.invoke(''addClassName'', ''myClass'');


Thinking further, there are 65 Element.Methods. The passive ones are only 5:


visible, match, hasClassName, empty, descendantOf


All the rest are either action methods (addClassName, toggle, identify - we
have invoke for these), DOM traversal ones (up, next, ancestors - those are
usually invoked on a single element) and property related (getHeight,
getStyle, getOpacity). The latter ones can be collected either way:


someCollection.map(Element.Methods.getWidth)
//or
someCollection.collect(function(el){return
el.getStyle(''opacity'')})


Just trying to figure out if it really makes sense to have it in a core.


Best,
http://thinkweb2.com/projects/prototype/ kangax
--
View this message in context:
http://www.nabble.com/Extension-for-writing-concise%2C-expressive-code-tf4497907.html#a12831702
Sent from the RubyOnRails Spinoffs mailing list archive at Nabble.com.