P.Brian.Mackey P.Brian.Mackey - 1 month ago 17
jQuery Question

Can jQuery be used a module pattern of some kind?

I saw this blip of code in someFile.js:

var someObject = {};

someObject.doWork = function(){
var innerFunction = function() {
console.log('innerFunction has been called');
}

innerFunction();
}

$(someObject.doWork);


This code is a piece of a bundle belonging to a durandal project. Is this some kind of module pattern? It appears jQuery is being used to invoke a function.

Is there any reason to do this over the more common IIFE?

someObject.doWork = (function(){
var innerFunction = function() {
console.log('innerFunction has been called');
}

innerFunction();
})();


To be clear, in the actual code
someObject
lives in the global namespace.

Answer

Cannot gather a reason to use that particular pattern. No value, nor original object is returned from .doWork(). $(someObject) is not defined as a variable; at least atjavascript at Question.

The reason to pass an object to jQuery() would be to utilize the jQuery methods on the object, where this[0] would be the original object at a callback; e.g.,

$(someObject).animate({someObject.property:someObject.property})

See Make a rotate animation: start and end slowly, but fast in the middle