A
A
Arman2015-08-30 14:31:49
JavaScript
Arman, 2015-08-30 14:31:49

Is it worth it to do methods for a jQuery plugin?

Hey; I'm not very friendly with the client side, and one of the reasons is cross-browser compatibility. Now I want to sketch a small plugin for jQuery. I read a bunch of articles and recommendations, and everywhere they make public methods through specifying the first argument and an array property with methods, where the key is the name of the method.
habrahabr.ru/post/158235

(function( $ ){

  var methods = {
     init : function( options ) {

       return this.each(function(){
         $(window).bind('resize.tooltip', methods.reposition);
       });

     },
     destroy : function( ) {

       return this.each(function(){
         $(window).unbind('.tooltip');
       })

     },
     reposition : function( ) { 
       // ... 
     },
     show : function( ) { 
       // ... 
     },
     hide : function( ) {
       // ... 
     },
     update : function( content ) { 
       // ...
     }
  };

  $.fn.tooltip = function( method ) {
    
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Метод с именем ' +  method + ' не существует для jQuery.tooltip' );
    }    
  
  };

})( jQuery );


In general, it is interesting, but is it possible to do this:
$.fn.tooltip = function()
    {
    this.destroy = function() {};
    this.reposition = function() {};
    this.show = function() {};
    this.hide = function() {};
    this.update = function() {};
    this.init = functin

    // код init
    // ..
    return this;
    };
})( jQuery );
// запуск
$.fn.tooltip().show();


It turns out that the plugin initialization itself initializes / creates an object and methods are already registered in it. After they can be called quickly and beautifully;
Or could there be some problem with it? They don't do that? So it doesn't work? Or what?
If you don’t need to do this, but you need to, as in the article, then how can you call other methods inside init (), say show ()?
Thanks =)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danila Simonov, 2015-08-30
@Arik

The answer to the last question is simple - there is access to methods inside init, just call it via .call(this, ...).
The implementations given in your example have a very big difference - in one case, the methods are stored only in one place - the methods object, in the other case - they are written to each object. The difference is the same as between methods in prototype and methods that are written to this in the constructor - in the first case, they are written in only one place, in the second, their implementations are written to each object.
Simply put, it's just very suboptimal in terms of memory consumption.

S
Sergey Sergey, 2015-08-30
@hahenty

In the first option, the list of methods is hidden from external changes, access is only by line in the plugin function. In the second option, the methods of the object created by the plugin can be replaced or deleted somewhere in the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question