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 8774389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:33:19+00:00 2026-06-13T18:33:19+00:00

I have just started to write JavaScript after some time and I am confused

  • 0

I have just started to write JavaScript after some time and I am confused by plugins constructor set up. I went through this link for advice.

What does the exclamation mark do before the function?

I am interested in understanding this function set up..

I have seen seen three examples like this below:

(function(parameterOne, parameterTwo) {

    functionOne = function(...) {
       ...
   },

   functionTwo: new function() {
   },

   this.functionThree = function(...) {
   }

})()

My questions are as follows:

  1. When do one use the construct as given for functionOne vs functionTwo vs functionThree? How would I be able to call the functionOne, functionTwo or functionThree explicitly?

  2. I have seen functions like this:

    (function(parameter) {
    
        functionFour = function(..) {
       },
       ....
    })(document)
    

What does the document in this example indicate? I have been confused about this for a long time now.

  1. I have seen jQuery plugins that follow this structure?

    (function($) {
    
      $.fn.myCustomPlugin: function(...) {
      }
    })(jQuery)
    

Why is $ passed to the function and jQuery passed at the end? I apologize as this may be rudimentary questions to some of you but these have stymied me for couple of weeks now and I am desperate to get any answers I can.

Thanks,

Kartik

  • 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-06-13T18:33:20+00:00Added an answer on June 13, 2026 at 6:33 pm

    (function(parameterOne, parameterTwo) {

    Does not make sense as you’re not passing these arguments in the end. Also, looks like you’re immediately trying to create anonymous code block – makes sense in some cases, but you don’t need to do it unless you’re trying to protect global scope from whatever happens inside this code block.

        functionOne = function(...) {
           ...
       },
    

    This is a named function similar to var functionOne will be useful only inside parent function or constructor.

       functionTwo: new function() {
       },
    

    UPDATE: Doesn’t really make sense as you’re trying to create instance here. You can definitely immediately instantiate what you have inside that function declaration (it is same as declaring a func. first and then using operator new) but in this case what you get is an object not a function. Try:

    var f = function() {};
    console.log(f); // "function"
    var f = new function() {}
    console.log(f); // "objest"
    

    That object is the instance of your constructor function.

       this.functionThree = function(...) {
       }
    

    this is a method of a class. Once you instantiate the class using operator new, you can access it from outside of the instance. Also when you use this. inside a class, remember that every time you instantiate a class, it will reserve memory for this value. When the variable is different for these instances than it’s okay, but if it’s a method, you’re better of adding it to the constructor prototype, this way it’s only declared once and new memory chunks are not reserved for it.

    })()
    

    here you would ordinarily pass params you request inside, e.g. parameterOne, parameterTwo

    (function(parameter) {
    
        functionFour = function(..) {
       },
       ....
    })(document)
    

    This is a way to pass context or any objects to this anonymous code block. In this case it looks like a plug in that takes window.document but it can also take any other part of DOM. For instance, if you want to select all tags a and receive document as param, it will iterate through all links in window.document, but if you pass $('#somediv'), this plugin will iterate through links only inside div with this id.

    (function($) {
    
      $.fn.myCustomPlugin: function(...) {
      }
    })(jQuery)
    

    This is a good way to make sure that in the code inside $ is a jQuery object. Thing is that in Chrome for example there’s already a native function $, or in some production sites $ may be a Prototype JS function. But in the example inside you make sure that var $ is not native $ or not Prototype.

    In general, you should not use (function() {...})() liberally — this is only used in specific cases when you’re protecting global environment as I mentioned above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started Entity Framework & linq and write this query var query
I have just started learning Javascript. I want Hello World! to be written to
I have just started learning to write apps for android using Eclipse. Where can
I am just getting started with HTML/JavaScript, and I have a simple question. I
I have just started using CodeContracts with C#, and I might need some help.
I'm new to XNA game development and I have just started to write a
I have just started using Sharepoint Foundation 2010 and I'm trying to write a
Started learning javascript yesterday at CodeAcademy and decided I could actually write code. Just
Just started working on a basic grid analysis algorithm in JavaScript but I have
we have just started using a git account of our Django website project so

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.