7

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 Answer 1

4

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;
5
  • So if I understand you correctly, a 'direct' function on type A overwrites the previous prototype function with the same name?
    – pimvdb
    Mar 22, 2011 at 21:02
  • 1
    @pimvdb No - a getWindow method is added to the instance object itself (which is a in the code above). The A.prototype.getWindow method is not modified. However, a.getWindow now shadows the A.prototype.getWindow method, so that this prototype method becomes unaccessible to the instance object a. Mar 22, 2011 at 21:05
  • why I need '()' in the end of the 'return'?
    – theateist
    Mar 24, 2011 at 13:50
  • 'win' was declared as LOCAL variable. Shouldn't it to be destroyed once it's returned from the PROTOTYPE getWindow and thus returning from the INSTANCE getWindow should return undefined, isn't it?
    – theateist
    Mar 27, 2011 at 9:00
  • @theateist No, the win variable will survive the first getWindow call - because of closure. In JavaScript, if a variable is used by an inner function, it will not be destroyed as long as the inner function exists. In this case, the inner function is assigned to this.getWindow as a permanent property - this means that the inner function will never die. And since the inner function is using the win variable, it (the variable) will also never die. Mar 27, 2011 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.