JavaScript prototyping tutorial

functiongetelementbyidjavascriptprototyping

I have a JS script with tons of functions, on big on is this:

function id(i) {
    if(document.getElementById)
        return document.getElementById(i);
    return;
}

It just saves me many bytes in typing out document.getElementById() each time.
My problem is I frequently add and remove classes and other attributes from elements.
I want to be able to do this:

id('main').addClass("someClass");

Does anyone know any good Javascript Prototyping tutorials which can explain this?
Thanks.

Best Solution

the technique you're looking for is method chaining. basically, the "id" function should return an object instance that has the "addClass" method on it. and that method would "return this".

Here's a tutorial that explains this concept:
http://javascriptant.com/articles/32/chaining-your-javascript-methods

I would also highly recommend this book to learn more techniques such as this (yes chaining is covered) :-)
http://jsdesignpatterns.com/