Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:22:40+00:00 2026-05-27T16:22:40+00:00

I came across the following pattern when looking through the SlickGrid source code: (function

  • 0

I came across the following pattern when looking through the SlickGrid source code:

(function ($) {
  var SlickEditor = {

    TextCellEditor: function (args) {
      ...
    },

    LongTextCellEditor: function (args) {
      ...
    }
  };

  $.extend(window, SlickEditor);

})(jQuery);

If I understand this correctly, it is using immediate invocation to define various function objects and then merge them into the global namespace.

So I could just define my functions globally like this and it would have the same effect, right?

function TextCellEditor (args) {
  ...
}

function LongTextCellEditor (args) {
  ...
}

The only difference I can see is that in the first version, I can use the $ shorthand to refer to the jQuery object. Apart from that the result would be identical in both cases.

I would like to know if I am missing something. Maybe there is another good reason for doing things this way?

UPDATE: Please note, I realise that using this immediate invocation pattern allows for the use of private variables declared in the anonymous function. But there aren’t any variables declared in this case and the functions are being injected into the global scope anyway. So I’d still like to know if there’s any real difference.

Some answers pointed out that referencing local variables is much faster than referencing global variables. Does this still hold true if I reference $from within the TextCellEditor() constructor. $ is not really local to TextCellEditor() since it is defined in the scope of the outer anonymous function.

All comments appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:22:41+00:00Added an answer on May 27, 2026 at 4:22 pm

    The difference is that you are creating a local scope for private variables when you use a self invoking function like the one you have shown.

    That type of self invoking function is usually called a closure and is the basis of many javascript patterns like the module pattern: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

    You can read more about javascript scoping here: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

    The reason for passing the jQuery vairable in is 2 fold:

    1: it’s more convinient to call it via $ and since it was passed in as a local variable we can be sure that $ isn’t something else

    2: it’s faster – local variables are much faster to access and work with than global variables

    As an example take a look at the following chunk of code:

    var public_and_global=true;
    
    (function(){
      var private_and_local = true;
    
      function local_func() {
       //can use private_and_local as well as public_and_global
      }
    
    })();
    
    function global_func() {
       //can only use public_and_global
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following code: var f = function () { var args
I came across following code function currentSlideFromParams() { var result; if (result = window.location.hash.match(/#([0-9]+)/))
I came across the following code fragment in a CouchDB book . function(doc) {
I came across the following expression in someone else's code. I think it's terrible
I came across the following regular expression below on one of my code, anyone
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following weird chunk of code.Imagine you have the following typedef:
I came across the following code in a code base I am working on:
I have came across the following code, and being a C beginner, I came
I just came across the following code: for(Iterator<String> iterator = stuff.iterator(); iterator.hasNext();) { ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.