Javascript: unable to execute init function

207 views Asked by At

I am working on a groupdocs viewer, and want to re-create plug-in like that,

groupdocs code below

(function ($) {
    var methods = {
        init: function (options) {
            debugger
            var defaults = {

            };
            options = $.extend(defaults, options);
        }
    }
    $.fn.viewer = function (method) {
        console.log(method)
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method' + method + ' does not exist on jQuery.viewer');
        }
    };
}(jQuery));

this code is working fine but when I create my custom code like below,

(function ($) {
    var methods = {
        init: function (options) {
            debugger
            var defaults = {

            };
            options = $.extend(defaults, options);
        }
    }
    $.fn.secondFunction = function (method) {
        if (methods[method]) {
           return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
       } else if (typeof method === 'object' || !method) {
           return methods.init.apply(this, arguments);
       } else {
           $.error('Method' + method + ' does not exist on jQuery.viewer');
       }
    };
}(jQuery));

secondFunction and event init: is not executing,

what might be the problem with my code

1

There are 1 answers

0
Olian04 On BEST ANSWER

Your code is working just fine. Open the inspector and run press the run button below, and you will see that it pauses on the debugger statement.

(function ($) {
    var methods = {
        init: function (options) {
            debugger
            var defaults = {

            };
            options = $.extend(defaults, options);
        }
    }
    $.fn.secondFunction = function (method) {
        if (methods[method]) {
           return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
       } else if (typeof method === 'object' || !method) {
           return methods.init.apply(this, arguments);
       } else {
           $.error('Method' + method + ' does not exist on jQuery.viewer');
       }
    };
}(jQuery));

$().secondFunction();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>