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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:24:00+00:00 2026-06-15T10:24:00+00:00

Following are two ways to define BW.Timer. Can someone tell me what the difference

  • 0

Following are two ways to define BW.Timer. Can someone tell me what the difference is? I am not certain the first is even valid, but if it is valid, what is different about using the myfunc=(function(){}()) syntax?

BW.Timer = (function () {
    return {
        Add: function (o) {
            alert(o);
        },
        Remove: function (o) {
            alert(o);
        }
    };
} ());

And…

BW.Timer = function () {
    return {
        Add: function (o) {
            alert(o);
        },
        Remove: function (o) {
            alert(o);
        }
    };
};
  • 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-15T10:24:01+00:00Added an answer on June 15, 2026 at 10:24 am

    The first is the return-value of the immediately-invoked function. The second is a function. It essentially comes down to what the difference is between these:

    var f = (function() { return 0; })();
    
    var f = function() { return 0; };
    

    Since the first function is called immediately, the value of 0 is given to the variable f. The first f is not a function. However, the second f we must call in order to get the value:

    f(); // 0
    

    It follows that in your example, the first BW.Timer is the object literal itself and the second is a function returning an object literal. You must call the function in order to get to the object:

    BW.Timer().Add(x);
    

    Why use the first then?

    You might ask yourself why one would use a syntax like a = (function() { return {}; })() instead of a = {}, but there’s a good reason. An IIFE (Immeditately-Invoked Function Expression), unlike a regular function allows the emulation of static variables (variables that maintain their value through a single instance). For example:

    var module = (function() {
        var x = 0;
    
        return {   get: function()  { return x },
                   set: function(n) { x = n }
        };
    
    })();
    

    The above a text-book example of the Module Pattern. Since the function is called right away, the variable x is instantiated and the return value (the object) is given to module. There’s no way we can get to x other than by using the get and set methods provided for us. Therefore, x is static, meaning its variable won’t be overridden each time you use module.

    module.set(5);
    
    module.get(); // 5
    

    On the other hand, let’s see an example where module is declared as a function instead:

    // assume module was made as a function
    
    module().set(5);
    
    module().get(); // 0
    

    When we call module() the x variable is overridden each time. So we’re effectively using different instances of module and x each time we call module.

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

Sidebar

Related Questions

what's the difference for the following two ways to define namespace? namespace A.B.C {
Is there any difference between following two ways of creating an object. Student s1
Will there be a difference between the following two ways of calling a function
Given the following two ways of defining a class method in Ruby: class Foo
Look at the following two ways to create a new object of class Y:
I have the following as two ways to declare a two dimensional String array.
I have the following two chunks of code, I am not so sure what
In the following code, the const are defined using two different ways. const float
In open source projects I see the following two ways of writing specs: Specs
I have the following two ways suggested to me. window.location.href = '/Administration/Notes/Create?dsValue=a&selectAnswer=b'; $.get(/Administration/Notes/Create, {

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.