For a Javascript library, what is the best or standard way to support extensibility

Posted by Michael Best on Programmers See other posts from Programmers or by Michael Best
Published on 2012-06-30T00:39:49Z Indexed on 2012/06/30 3:23 UTC
Read the original article Hit count: 271

Filed under:
|
|
|

Specifically, I want to support "plugins" that modify the behavior of parts of the library.

I couldn't find much information on the web about this subject. But here are my ideas for how a library could be extensible.

  1. The library exports an object with both public and "protected" functions. A plugin can replace any of those functions, thus modifying the library's behavior. Advantages of this method are that it's simple and that the plugin's functions can have full access to the library's "protected" functions. Disadvantages are that the library may be harder to maintain with a larger set of exposed functions and it could be hard to debug if multiple plugins are involved (how to know which plugin modified which function?).

  2. The library provides an "add plugin" function that accepts an object with a specific interface. Internally, the library will use the plugin instead of it's own code if appropriate. With this method, the internals of the library can be rearranged more freely as long as it still supports the same plugin interface. This could also support having different plugin interfaces to modify different parts of the library. A disadvantage of this method is that the plugins may have to re-implement code that is already part of the library since the library's internal functions are not exported.

  3. The library provides a "set implementation" function that accepts an object inherited from a specific base object. The library's public API calls functions in the implementation object for any functionality that can be modified and the base implementation object includes the core functionality, with both external (to the API) and internal functions. A plugin creates a new implementation object, which inherits from the base object and replaces any functions it wants to modify. This combines advantages and disadvantages of both the other methods.

© Programmers or respective owner

Related posts about JavaScript

Related posts about api