问答详情
源自:2-1 如何实现jQuery插件框架

2-1 12:10秒处有疑问

$.fn.PageSwitch = function(options) {
                return this.each(function() {
                    var $me = $(this);
                    var instance = $me.data("PageSwitch")
                    if (!instance) {
                        instance = new PageSwitch($me, options);
                        $me.data("PageSwitch", instance)
                    }

                    $('div').PageSwitch('init')   这是怎样调用init方法的??这样写不是将init作为参数传递进去了?

                    if ($.type(options) == "string") {
                        return instance[options]();
                    }
                })
            }

提问者:慕粉1467970169 2016-12-11 20:07

个回答

  • 姜芽儿
    2017-02-07 18:32:09

     if ($.type(options) == "string") {
            return instance[options]();
        }

    这里他在演示如果需要调用init方法需要怎么做。

    他这里有判断如果传进去是个字符串的话,就调用实例上面的对应的方法。

    instance[options]();

    这里instance是instance = new PageSwitch($me, options); 

    实例可以调用构造函数prototype上的方法。