JavaScript prototyping tutorial


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

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

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:


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

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:

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