[whatwg] Re: getElementsByClassName

Kornel Lesinski kornel at ideadesigners.com
Tue Sep 6 02:12:38 PDT 2005


On Tue, 06 Sep 2005 00:54:56 +0100, Ian Hickson <ian at hixie.ch> wrote:

>> > I fear that if we use a string that must be parsed, we will encourage
>> > buggy implementations.
>>
>> Not at all. Actually this should improve implementations, because same
>> parsing algorithm is used for both input string and class attribute.
>
> Unfortunately in practice the two parts of that code are likely to be
> completely unrelated parts of the codebase, so that reuse is unlikely.

Even if it isn't reused, such function is not a rocket science. Can't you  
trust implementors to trim and split string properly?


>> For example I may want first to find set of classes I'd like to match
>> against. With solution I propose it's easy and intuitive to anyone who
>> used .className:
>>
>> if (x) find += " class1";
>> if (y) find += " class2";
>> getElementsByClassName(find);
>>
>> but with varargs function it's really cumbersome:
>> if (x) find.push("class1");
>> if (y) find.push("class2");
>> switch(find.length)
>> {
>>  case 1: getElementsByClassName(find[0]); break;
>>  case 2: getElementsByClassName(find[0],find[1]); break;
>>  ...
>> }
>
> You can just do:
>
>   if (x) find.push("class1");
>   if (y) find.push("class2");
>   document.getElementsByClassName.apply(document, find);
>
> ...which seems much better to me than using a string.

It's the first time I see apply method used. I couldn't find it in  
ECMA262-3 nor in WA1.0. Can you give me a hint where it's defined?
Why is that better than using string?

-- 
regards, Kornel Lesinski



More information about the whatwg mailing list