Various approaches which Javascript libraries follow in their implementation

Javascript libraries, like jQuery, MooTools are a boon to developers. They help in making a developer’s life easy by not only providing multiple utility functions but also handling cross browser inconsistencies.

Most of these libraries take one of the following approach in their implementation:

a. Extending Native Javascript objects

b. Creating wrapper object/function to expose various utility functions

c. Combination of approach (a) and (b)

Libraries like prorotype.js extend native object alot, where as, jQuery is into wrapper object.

When extending native Javascript objects, libraries tend to modify in-built objects like Array, String etc.. and add utility functions to them. This allows a developer, using these libraries, to treat such functions as if they were a part of the object they are operating upon, something like following:

“my string”.myNewFunction();

Where as, when a wrapper object or function is created, then utility functions are associated and exposed to this particular wrapper in various manners, like $ in jQuery.

Both the approaches have their pros-cons.

One school of thought opposes extending of native objects, considering to be against the rule of not modifying objects which you don’t own. The other school of thought find this approach to be more Javascript-way, in which the object can be given more power as and when required.

If planning to create your own utility library, its great to keep these approaches in mind and choose the one you feel suits the vision of your library in the long run.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s