I think that depends on if the methods need to be available globally
of not.

This is also up to personal preferences. I personally don't like
polluting the window namespace with unnecessary stuff.
Especially since jQuery is so easy to extend.

JQuery also provides methods of encapsulating data or objects (eg. $
(this).data("currentX", 125) that almost eliminates the need for
multiple global variables.

If any variable need to be available globally I usually either use an
element as a data container, or store the data in the window in
jQuerys context. (eg. $(window).data("currentwhatever", "current data
or obj").

Often it is just easy to just create functions and variables in the
global (window) scope and be off running with them.

But if you are working directly with jQuery objects with your
functions sometimes you are better off at least using anonymous
functions that encapsulate the methods needed.

If a method or variable does not have to be in a global context it
should not be there.

Personally I create most of my common functions within an anonymous
function, and I extend jQuery and jQuery.fn anytime I am working with
any method that has to be used by jQuery in any complex way.

eg:
(function ($) { // anon function

  var toupper = function(a){ anon function only available within this
context
            return a.toUpperCase()
          };

  $("#something").click(function(){
       $(this).attr("title", toupper( $(this).attr("title") )
           );
  });

})(jQuery);

I also don't like polluting jQuerys namespace unneccisarily.
If there are several methods that are all doing parts of the same
common objective I try to create 1 namespace in jQuery and use
sub.methods within the $.extension.

This way I am only using 1 namespace within jQuery and I can call my
methods like this.

$.mymethod.foo(); $.mymethod.bar();

$.extend({

         mymethod:{
          foo: function(){
                         },
                  bar: function(){
                         },
                  foobar: function(){
                         }
     }
});


I am sure people have different ways of doing this stuff but that is
my 2 pence.





On May 25, 3:50 pm, kiusau <kiu...@mac.com> wrote:
> QUESTION:  I once read in this forum that it is a good idea to limit
> the number of "new" jQuery methods and write as much script as
> possible outside of the jQuery framework.  If this is true, then how
> does one go about assigning functions to tags, if they are not
> themselves jQuery methods that one creates.
>
> BACKGROUND:  In my eagerness to get up to speed quickly I began
> turning every function into a jQuery method.  This may be contributing
> to some of my difficulty.
>
> Roddy

Reply via email to