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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:51:18+00:00 2026-05-20T18:51:18+00:00

I see a lot that function returns NOT the result but the function. The

  • 0

I see a lot that function returns NOT the result but the function.
The example below shows that function getWindow returns function. Why it can’t just return variable “win”? When I return result and when function?
Thank you.

var A = function(){};
A.prototype=
{
   getWindow : function()
   {
        var win = new B.window();
        return (
                this.getWindow = function()
                {
                    return win;
                })();

   }
}
  • 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-20T18:51:18+00:00Added an answer on May 20, 2026 at 6:51 pm

    This code is equivalent to your code but is easier to understand:

    A.prototype = {
        getWindow: function() {
    
            var win = new B.window();
    
            this.getWindow = function() {
                return win;
            };
    
            return win;
    
        }
    }
    

    Usage:

    First, create an A instance:

    var a = new A();
    

    Then, call getWindow on that instance:

    a.getWindow();
    

    Here, the getWindow method of A.prototype is called. As you can see in my code above, A.prototype.getWindow will create a new B.window() and return it, however in between, it will also create a getWindow method on the instance object itself.

    Now, if you call getWindow again:

    a.getWindow();
    

    A.prototype.getWindow is no longer called because the instance object itself has a getWindow method. This method returns the same “win” object that was returned when the getWindow method was called for the first time.


    Your pattern allows for multiple A instances to use the same A.prototype.getWindow method to instantiate their own “win” objects. Consider this:

    var a1 = new A,
        a2 = new A,
        a3 = new A;
    
    a1.getWindow(); // creates window W1 and returns it
    a2.getWindow(); // creates window W2 and returns it
    
    a1.getWindow(); // returns window W1
    a2.getWindow(); // returns window W2
    
    a3.getWindow(); // creates window W3 and returns it
    
    a1.getWindow(); // returns window W1
    a2.getWindow(); // returns window W2
    a3.getWindow(); // returns window W3
    

    This is quite a useful pattern :)


    Update:

    This is your code:

    return (this.getWindow = function() {
        return win;
    })();
    

    First, let’s take a look at the expression inside the parens:

    this.getWindow = function() { return win; }
    

    As you can see, it’s an assignment expression. An anonymous function object is assigned to the getWindow property of the object referenced by this (the instance object).

    Note that this function returns the win object.

    The result of this assignment expression is the function object itself! This means that the value inside the parens is the function object.

    Now, let’s take a look at the whole picture:

    return ( the_function_object )();
    

    We can remove the parens since we don’t need them anymore:

    return the_function_object();
    

    As you can see, the function object is called, and then the return value of that function is returned.

    As mentioned above, the function returns win. Therefore, the code resolves to this:

    return win;
    

    So what your code does is:

    FIRST, it assigns function() { return win; } to this.getWindow.

    SECOND, it returns the result of calling that function which is win.

    My code produces the same result but is easier to understand:

    this.getWindow = function() {
        return win;
    };
    
    return win;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see lot of example opening popup with jquery but don't see anything that
I see a lot of objective-c code that just #defines local constants it needs,
I see a lot of example code for C# classes that does this: public
I've been using C/C++ and Python, but I now I see that a lot
I've been trying to troubleshoot this, but can't find a good function that will
I see a lot of c++ code that looks like this: for( const_iterator it
Looking through my server logs, I see that a lot of pages on my
As I am learning Haskell, I see that there is a lot of language
I need to create a view or a table valued function that returns one
I want to access a C function that returns a struct containing double arrays

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.