@Matt:
> I believe using the non-methodized version of this method would be
> better
>
> Element.hasClassName.curry(element);
Ah, yes, well spotted. Probably not a huge difference, but certainly
a positive one. E.g.:
if (['foo', 'bar', 'foob'].any(Element.hasClassName.curry(element))) {
// ...
}
@OP:
All of Prototype's element extensions are also available via
Element.xyz. E.g., these statements are functionally equivalent:
myElement.addClassName('foo');
Element.addClassName(myElement, 'foo');
The former (the "methodized" version) is just a wrapper for the latter
(and therefore incurs a tiny bit more overhead).
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
On May 14, 8:32 pm, Matt Foster <[email protected]> wrote:
> > var test = ['foo', 'bar', 'foob'];
> > if (test.any(element.hasClassName.bind(element))) {
>
> I believe using the non-methodized version of this method would be
> better
>
> Element.hasClassName.curry(element);
>
> --
>
> http://positionabsolute.net
>
> On May 14, 12:30 pm, "T.J. Crowder" <[email protected]> wrote:
>
> > Hi,
>
> > I can think of two approaches off-hand:
>
> > 1. Use Enumerable#include on the array of classes you want to test
> > against, using Element#hasClassName as the iterator function (bound to
> > the instance), e.g.:
>
> > var test = ['foo', 'bar', 'foob'];
> > if (test.any(element.hasClassName.bind(element))) {
> > // ...
> > }
>
> > or
>
> > if (['foo', 'bar', 'foob'].any(element.hasClassName.bind
> > (element))) {
> > // ...
> > }
>
> > (You don't need to use $A() around array literals.)
>
> > 2. Use $w on the class name to get an array of the individual class
> > names, then use Enumerable#any on that, passing in Enumerable#include
> > on the array you want to test against as the #any iterator. Something
> > like this:
>
> > var test = ['foo', 'bar', 'foob'];
> > if ($w(element.className).any(test.include.bind(test))) {
> > log("true");
> > }
>
> > (Both of those bits of code are untested.)
>
> > But if this is going to be happening a lot (a tight loop, or a
> > mouseover handler, etc.), check the runtime cost of each of these by
> > going under the covers.
>
> > FWIW,
> > --
> > T.J. Crowder
> > tj / crowder software / com
> > Independent Software Engineer, consulting services available
>
> > On May 13, 7:34 pm, louis w <[email protected]> wrote:
>
> > > I have an array containing a number of strings. I would like to
> > > continue exucuting my script only if an item has ANY/ALL of the
> > > strings assigned as a class name.
>
> > > $A(['foo', 'bar', 'foob']);
>
> > > Is there an elegant want to do this without having to loop through the
> > > items?
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---