Last Updated: February 25, 2016
·
1.321K
· 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");
});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside