简体   繁体   中英

How to extend javascript library through plugins?

Say i want to develop a library that will keep a very small core and may be extended through plugins.

So i basically came up with something in the scale of:

(function(window){
     window.SomeLibrary =(function(){
          var api = { 
              init: function(){
                  console.log('hi');
                  privateFunction();
                },
              plugin: function(callback) {
                  callback.call();
                }
            };
          var privateFunction = function() {
            console.log('im a private method');
            };
        return api;
        }());
    })(window);

SomeLibrary.init();
SomeLibrary.plugin(function(){
    console.log('im a plugin');
    privateFunction(); //fails;    
});

How can i make the plugin callback executes SomeLibrary private and public methods?

Thanks in advance.

You can collect your private functions in an object and pass this object to the extending plugin.

Lib.extend('pluginName',function(privates){
    //I have access to privateJob via privates.privateJob();
});

then in lib:

var privateCollection = {                 //your private collecting object
    privateJob : function(){...}          //a private function
}

extend : function(pluginName,callback){ //an extend function that receives callback
    callback(privateCollection);          //execute callback with private collection
} 

but access to inner functions is a bad idea. this WILL wreck the internals of your library.

You can't. That's why they are private . They are not accessible from outside the scope.

If you expose some references to those methods, you need to treat those functions as properties or SomeLibrary , which means they are not private anymore.

Or you can use closure to achieve similar results. But I don't think it's private either.

The privateFunction is not in the scope for the callback. By using a closure you can make the function available for plugins, without providing it in the api , but the function will not be private .

For example:

(function(window){
 window.SomeLibrary =(function(){
         var privateFunction = function() {
            console.log('im a private method');
        };
         var init = function(){
              console.log('hi');
              privateFunction();
            };
          var plugin = function(){ 
              var pluginScope={
                  privateFunction :privateFunction,
              };
              return function(callback) {
                  callback(pluginScope);
               }
          }();
          var api = { 
              init: init,
              plugin: plugin
          };
    return api;
    }());
})(window);

SomeLibrary.init();
SomeLibrary.plugin(function(scope){
    console.log('im a plugin ');
    scope.privateFunction();;    
});​

You can test it at http://jsfiddle.net/uXppZ/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM