2015-07-28 52 views
0
;(function($) { 

    var Sanbox = { 
    init: function(options, elem) { 
     self = this; 

     // combine default options and user passing 
     self.options = $.extend({}, $.fn.sanbox.options, options); 
     console.log(this);   // an instance of Sanbox() object the caller 
    }, 
    greet: function() { 
     console.log('work'); 
    } 
    }; 

    // create your sanbox plugin 
    $.fn.sanbox = function(options) { 
    return this.each(function() { 
     var sanbox = Object.create(Sanbox); 
     sanbox.init(options, this); 
    }) 
    } 

    // plugin default options 
    $.fn.sanbox.options = { 
    name : 'byer', 
    age : 24, 
    address : 'Lorem ipsum dolor sit amet.' 
    }; 

})(jQuery); 

// use 

任何方式我可以使用/訪問jQuery插件定義之外的greet()方法嗎?我可以使用/訪問jQuery插件定義之外的greet()方法嗎?

而在init()方法裏面,這個是指什麼?

回答

0

要在插件定義之外使用greet(),您需要將其公開爲插件的公共功能。這個問題說明如何實現它: jQuery plugin creation and public facing methods

裏面的init()方法this是具有原型Sanbox的對象。你用Object.create(sanbox)創建了這個對象。

相關問題