编写jQuery插件的构造函数

时间:2014-08-08 18:09:37

标签: javascript jquery html html5

如何为jQuery插件编写构造函数?为什么this.Viewer.init()不能在这里工作?

;(function($){
    $.fn.testing= function(options){
        var settings = {
            'url'            : null
        };   
    if(options){$.extend(settings, options)};    
    var Viewer = {
        init: function(){
            console.log('Lorem ipsum dolor');
        }
    };    
    };
    this.Viewer.init();
})(jQuery);

CONSOLE.LOG

Uncaught TypeError: Cannot read property 'init' of undefined 

1 个答案:

答案 0 :(得分:2)

当您访问Viewer而不需要使用Viewer时,您将this定义为本地变量而非属性:

Viewer.init();