0

I created a plugin but want to be sure about what I do with "local" functions.

Here is schematically what I did :

(function($) {

 var methods = {
     init : function( options ) {

       // CODE ...

       // Call of a local function
       _test( this );

       // CODE .....

     },
     destroy : function( ) {       
         // CODE .....
        _test( this );
         // CODE .....
     }
  };

  function _test( container ) {
       // My code : example :
       $(container).append("<div id='myplugin'></div>");
  }

 $.fn.myplugin = 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 ' +  method + ' does not exist on jQuery.myplugin' );
    }    

  };

})(jQuery);

As you can see, I don't directly insert the code in the methods functions but in other _functions. Is the _functions can be considered as local or private function of the plugin ? I don't success to call them outside of the plugin, so it seems for me they can be considered private functions...

Do I have always to put my code in the function in the methods object directly ? How to declare functions which will be used in several methods ?

And what about the namespace ? Don't really understand.

Thanks !

bastien
  • 209
  • 2
  • 14

1 Answers1

0

Since you are exposing all methods of the methods object whatever is part of that object will not be private. However any function declared inside another function is scoped to the declaring function so if you do not otherwise make the function accessible, then it will be private.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • I'm not sure to understand everything. – bastien Jun 11 '12 at 18:08
  • @bastien what parts do you not understand? – Rune FS Jun 11 '12 at 18:09
  • I understand that functions inside declaring function are private. The methid of my method object are public in order to be called in the code of my web page. But, I don't understand why the functions I call in the method of the method object are not private... I think it is impossible to call them directly externally from the plugin. Moreover, what is the solution, if I want to use a function in more than one method of the method object ? – bastien Jun 11 '12 at 20:03
  • @bastien In your code you simply forward the call, so the only thing you've encapsulated is the name of the function being called – Rune FS Jun 11 '12 at 20:23
  • Yes, I maybe didn't put the right name for the answer. Imagine instead of _init() in the init method, I have the function test(), which will be also used in the destroy method. So I need to declare the test function out of the method object. No ? Or is there another method to do that ? – bastien Jun 11 '12 at 20:42
  • After reading lots of different solutions and advices, I think the one which is the clearest is this one : http://stackoverflow.com/a/7661010/1334924 I think I will use a private_methods object, like the methods object for public, and my code will be clear and ok. – bastien Jun 12 '12 at 13:30