2015-07-28 54 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)创建了这个对象。

相关问题