Javascript: невозможно выполнить функцию инициализации - PullRequest
0 голосов
/ 19 марта 2020

Я работаю в средстве просмотра groupdocs и хочу воссоздать плагин следующим образом:

code groupdocs ниже

(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));

этот код работает нормально, но когда я создаю мой пользовательский код, как показано ниже,

(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 и событие init: не выполняется,

в чем может быть проблема с моим кодом

1 Ответ

0 голосов
/ 19 марта 2020

Ваш код работает просто отлично. Откройте инспектор и запустите, нажмите кнопку запуска ниже, и вы увидите, что она останавливается в операторе отладчика.

(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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...