Last Updated: February 25, 2016
·
1.12K
· phantom

Jquery extender - the fastest way to create custom plugin

Jquery extendable is fastest and simpliest way to create a jquery plugin.
https://github.com/igorzg/jquery-extendable

Example:

(function($){
    /**
     * Constructor 
     */
    function Test(options, selector){
        this._settings = $.extend({
            a : 'a',
            b : 'b'
        }, options);
        this._selector = $(selector);
        this._init();
    }

    /**
     * Extend prototype
     */
    Test.prototype = {
        /**
         * Initalize
         */
        _init : function(){

        },
          /**
         * Private
         */
        _private : function(){
            throw new Error('Not visible');
        },

        /**
         * Public method
         */
         pub : function( a, b, c){
            return a + b + c;
         }
    }

   $.fn.Test = $.extendable(Test);
}(jQuery));

Run example:

$(function(){
    $('div.box').Test({
        a: 'works',
        b : 'works_also'
    });
    //call pub method
    $('div.box').Test("pub", 1 ,2 ,3 );
     //not accessible and will not throw an error
    $('div.box').Test("_private");
});