JavaScript return method

Go To StackoverFlow.com

4

I'am new in javascript. I can't understand why the function returns T1 object (not just string 'hi') in the following example.

 function T1(){
    return 'hi';
 }
 function T(){
    return new T1();
}
T();

output: T1

And returns function in the following example

 function T1(){
    return function(){ return 'hi'; }
 }
 function T(){
    return new T1();
}
T();

output: function (){ return 'hi' }

Why does the first example returns an object (not the string "hi", what is expected to happen) and the second returns the function body that is returned from the first function (not the expected object)?

Please explain this result. Thank you)

2012-04-04 23:24
by user1314034


4

The new operator returns the object created by the operator unless the constructor function returns a different object. Any non-object return value of the constructor function is ignored, which is why when you return hi you don't see this.

Section 13.2.2 ("[[Construct]]") of the specification, which is referenced by Section 11.2.2 ("The new operator").

2012-04-04 23:27
by Chris Gessler
In the second example I also use "new". But function is returned. Why - user1314034 2012-04-04 23:29
@user1314034 In Javascript a function is a type of object, so the explanation above still applies even though the function doesn't look like it is returning an objec - wheresrhys 2012-04-05 11:31
Chris Gessler, your answer was the best! tnx - user1314034 2012-04-05 12:09


3

The new keyword. That makes an object.

 function T1(){
    return 'hi';
 }
 function T(){
    return T1();
}
T(); // 'hi'

JavaScript objects should be like this:

 function T1(){
    this.test = function(){
        return 'hi';
    };
 }
 function T(){
    return new T1();
}
var test = T(); // T1 object
test.test(); // 'hi'
2012-04-04 23:26
by Rocket Hazmat
Ok! But i need new object of T1. And in the second example I also use "new". But function is returned. Why - user1314034 2012-04-04 23:33
@user1314034: When you make a new object, the function shouldn't return anything. See the 2nd example, I just added - Rocket Hazmat 2012-04-04 23:51
@user1314034—the expression new T1() doesn't return a function, it returns a plain object - RobG 2012-04-04 23:55


1

In your code:

> function T1() {
>      return 'hi';
> }

A function called as a constructor will always return an object, it won't return a string. So when called simply as a function, T1 will return the string 'hi', but when called as a constructor, it iwll return a new T1 instance. Since that instance isn't assigned any properties, nor does T1.prototype seem to have any, it will be almost indistinguishable from an object created using new Object().

It will be an instanceof T1 and its constructor property will reference T1.

> function T() {
>      return new T1();
> }

By convention, only constructors start with a capital letter (except for constants, which might be all capitals).

> T(); 

The T function simply returns a new T1 object, which is not assigned to anything so can't be referenced or called, so it's immediately available for garbage collection. It has not special methods or properties.

var t = T(); // returns a new T1 instance
t();  // throws an error since t is a plain object, 
      // not a function and so can't be called.

Edit

Since the question was changed:

function T1() {
     return function(){ return 'hi'; }
} 

In this case, when T1 is called as a constructor, the return statement returns a function, which is an Object (all functions are objects), so that is what is returned.

2012-04-04 23:52
by RobG
The question is: Why does the first example returns an object (not the string "hi", what is expected to happen) and the second returns the function body that is returned from the first function (not the expected object)?rcdmk 2012-04-04 23:54
That is explained by the very first sentence of the second paragraph. Neither bit of code returns a function. If T1 is called as a function, it returns a string. If called as a constructor, it returns a plain object - RobG 2012-04-05 01:45
Ok, ok. So why haven't you answered it on the first time? Well, enough of discussion. The question is already answered - rcdmk 2012-04-05 20:43
rcdmk—the OP changed the question - RobG 2012-04-06 11:32


0

The function is returned in the second example because you returned an anonimous function on T1() and so, you can execute it placing a pair of parentesis () in front of if:

function T1(){ 
    return function(){ return 'hi'; } 
} 
function T(){ 
   return new T1(); 
} 
T()(); // hi
// or
var T2 = T();
T2(); // hi

This is not a bug, its a powerfull feature from JS. Just remember of "Spiderman" frase:

With great powers cames great responsabilities.

Edit:
Now I see the real question: Why does the first example returns an object (not the string "hi", what is expected to happen) and the second returns the function body that is returned from the first function (not the expected object)?

This seems a bug to me, but maybe by design, since functions and objects returned can be used and a workaroud with a parameter is easy to do.

2012-04-04 23:34
by rcdmk
Yeap. I can use T()(). But I can't understand why function can be returned, and the string is not - user1314034 2012-04-04 23:38
Sorry, now I see the point - rcdmk 2012-04-04 23:45
I've edited my answer with the real question. Maybe you can use it to get people stop answering like I did - rcdmk 2012-04-04 23:52
Thx! I've updated the question - user1314034 2012-04-05 00:00
Ads