jeudi 19 novembre 2015

How to make a javaScript library extendable with plugins

Let's say that i have this code as a javaScript library:

(function(global) {

  var _private = function(x) {
    return x;
  };

  var doSome = function(x) {
    return x;
  };

  var extend = function(x) {
    // ?
  };

  // maybe some inits....
  var init= function(x) {
    // ?
  };

  var myLib = {
    doSome: doSome,
    extend: extend,
  };

  // CommonJS, AMD, script tag
  if (typeof exports !== 'undefined') {
    module.exports = myLib;
  } else if (typeof define === 'function' && define.amd) {
    define(function() {
      return myLib;
    });
  } else {
    global.myLib = myLib;
  }

})(typeof global !== 'undefined' ? global : window);

I want to make this library extendable with plugins, plugins that use private methods

Aucun commentaire:

Enregistrer un commentaire