Creating a jQuery plugin: best practices regarding functions' visibility?

Posted by marcgg on Stack Overflow See other posts from Stack Overflow or by marcgg
Published on 2010-06-10T13:43:19Z Indexed on 2010/06/10 14:02 UTC
Read the original article Hit count: 243

I'm creating a jQuery plugin. So far it's working fine, but I'm having doubt about the way I'm doing things:

jQuery.fn.myMethod = function() {
  return this.each(function(){
    MyScope.doSomething(jQuery(this).attr("id"));
  });
};

var MyScope = {
  doSomething: function(id){
    // something
    doSomethingElse(23);
    // some more code
    doSomethingElse(55);
  },
  doSomethingElse: function(someInt){
    // some code
  }
};

I use MyScope to store all my "private" functions. I don't want the user to be able to go $("p").doSomething(), but I do need to use them.

I could move everything in the myMethod function, but it would create a 100 lines long function and people would hate me for it.

What's the best practices in this situation? Are there any great tutorials out there regarding this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery