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

Any way I can use/access the greet() method outside the jQuery Plugin definition?

And inside the init() method, what 'this' refer to?

1 Answers1

0

To use greet() outside of the plugin definition you need to expose it as a public function of the plugin. This question explains how you can achieve it: jQuery plugin creation and public facing methods

Inside the init() method this is an object which has a prototype of Sanbox. You created this object with Object.create(sanbox).

Community
  • 1
  • 1
andy250
  • 19,284
  • 2
  • 11
  • 26