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

  • Home
  • SEARCH
  • 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 6860813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:27:20+00:00 2026-05-27T02:27:20+00:00

What type of function is the following and which is their best use? var

  • 0

What type of function is the following and which is their best use?

var f = function (){
    var a = 0;
    return {
        f1 : function(){
        },
        f2 : function(param){
        }
    };
}();

I tried to convert it to:

var f = {
    a : 0,
    f1: function (){
    },
    f2: function (param){
    }
}

But seems does not works the same way.

  • 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-27T02:27:20+00:00Added an answer on May 27, 2026 at 2:27 am

    It’s just a plain old function that is invoked immediately, and returns an object which is then referenced by f.

    The functions referenced by object returned retain their ability to reference the a variable.

    No code outside that immediately invoked function can reference a so it enjoys some protection since you control exactly what happens to a via the functions you exported.

    This pattern is sometimes called a module pattern.


    Regarding your updated question, it doesn’t work the same because a is now a publicly available property of the object.

    For the functions to reference it, they’ll either do:

    f.a;
    

    or if the function will be called from the f object, they can do:

    this.a;
    

    Like this:

    var f = {
        a : 0,
        f1: function (){
            alert( this.a );
        },
        f2: function (param){
            this.a = param;
        }
    }
    
    f.f2( 123 );
    
    f.f1(); // alerts 123
    

    But the key thing is that a is publicly available. Any code that has access to f can access f.a and therefore make changes to it without using your f1 and f2 functions.

    That’s the beauty of the first code. You get to control exactly what happens to a.

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

Sidebar

Related Questions

i have to use the following function, to change the input's type <input class=input
I need a function with the following signature: System.Reflection.PropertyInfo getPropertyInfo(System.Type type, string NavigationPath) or
Hi I have the following code with jQuery: function reject(id) { $.ajax({ type: 'POST',
I have the following code that works on Firefox and Chrome $("#adicionarItem").click(function(){ $.ajax({ type:
I've seen some question here about almost the same thing, but, after following their
I am using the following function to read a .dmp file which contains user-defined
I have the following function which is part of the Trie structure implementation: int
I'm writing a function add_event as the following shows: function add_event(o, event_type, callback, capture){
I have the following code: $.ajax({ type: 'GET', url: 'index.php?route=checkout/onepagecheckout/getpaypaldata', dataType: 'json', success: function(json)
Can anyone explain the following PHP Code ? function get_param($param_name, $param_type = 0) {

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.