2

I've seen people add their own custom/user functions to the jQuery object ($). For instance:

$.myUserFunc = function() { /* regular JS code */}

Why would you do this? Instead of $.myUserFunc, why not create your function as simply myUserFunc or (if you want to avoid polluting the global scope) put it on another custom/user object like myObj.myUserFunc?

7
  • 2
    using myObj or myUserFunc doubles the global count compared to just $... Commented Aug 25, 2014 at 19:39
  • 3
    @Morklympious: only if they are jQuery expansions, otherwise you needlessly risk collision. Commented Aug 25, 2014 at 19:41
  • 1
    @dandavis I guess putting them on the $ global would make more sense as it doesn't result in naming collisions and it might very well give a common namespace for developers to throw their functions. Commented Aug 25, 2014 at 19:42
  • Related questions/answers: Why are global variables considered bad practice? and jQuery global variable best practice & options?. Commented Aug 25, 2014 at 19:44
  • 4
    in general, people do a lot of weird/dumb/slow/confusing stuff in JS, it doesn't always have a good reason behind it, but almost everything works. putting stuff on $ prevents globals, which a lot of noobs think is like the #1 most important thing. but, it also risks getting wiped out if another copy of jQuery or prototype is injected. it's probably harder to find stuff on $ than globals, and it tightly-couples what should be two separate pieces. i don't think it's a great idea, but it's not the worst thing ever. Commented Aug 25, 2014 at 19:45

1 Answer 1

3

The construct you are asking about:

$.myUserFunc = function() {}

creates a static function (available globally) on the jQuery namespace object. This is different from a jQuery plug-in method.

There are several reasons why one might do this - some are perhaps better justified than others.

  1. You may create a jQuery utility function that is directly related to jQuery (only useful when using jQuery), yet is at a different level than a plug-in method.

  2. You have a global function that you don't want to put in the global namespace (for all the typical reasons that lots of top-level global name are avoided) and you happen to be using jQuery so you choose to put your global function on the jQuery namspace object.

In my opinion, the first makes some sense. If it's a jQuery-specific function, yet it's a global function and not a method of a jQuery object (like plug-ins are), then putting it on the main jQuery makes some sense. There's really no reason to implement yet another top level namespace when there's an appropriate one already there and your function is related to that namespace object.

In my opinion, the second reason doesn't make as much sense from an architectural point of view. If this function isn't a jQuery-specific function (e.g. doesn't use or operate on jQuery objects), then it really doesn't belong on the jQuery object. It's a general purpose function and should be defined in a more general purpose way, either as a singleton global function or, if you have multiple types of related functions like this, put it on its own namespace object.


This said, in many cases, you can just create your own closure and define your own functions for use by your own code without having to put them either in the global namespace or on a namespace object. So, unless these functions must be globally accessible from anywhere, they don't need to go on the jQuery object.

For example, let's suppose you had two jQuery plugin-methods that wanted to share a common function. Obviously, this function has to be defined outside of these two plug-in methods. But, it does not have to be defined globally. I can be put in a closure and shared that way:

// define closure
(function() {

    // declare shared function in a closure
    function commonFunc() {
        // common code here
    }

    // define first plugin method
    jQuery.fn.plugin1 = function() {
        // do stuff
        // ....

        // use common function
        commonFunc();
    };

    // define other plugin method
    jQuery.fn.plugin2 = function() {
        // do different stuff
        // ....

        // use common function
        commonFunc();
    };

})();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.