How can the javascript plugin architecture in raphael/jquery be done?

Posted by TimDog on Stack Overflow See other posts from Stack Overflow or by TimDog
Published on 2010-06-03T00:33:45Z Indexed on 2010/06/03 0:54 UTC
Read the original article Hit count: 304

Filed under:
|
|

I'm looking for a barebones javascript example that demonstrates how the javascript plugin architecture works with large javascript libraries (such as raphael or jquery). In either scenario, you build plugins by ensuring your custom plugin follows this pattern: jQuery.fn.pluginName -- so assume I have a library:

myLibrary = (function() {
    //my fancy javascript code
    return function() {
        //my return object
    };
});

How would fn be incorporated into the above myLibrary object to ensure that he resulting plugin is callable? I instantiate myLibrary like so:

var lib = new myLibrary();

And now I have included a reference to my plugin in my page:

myLibrary.fn.simplePlugin = function() { //more fancy code }

So finally, I can just call:

lib.simplePlugin();

Basically, what magic is actually occuring when the .fn is used during the creation of the plugin?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about architecture