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)
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").
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'
new
object, the function shouldn't return anything. See the 2nd example, I just added - Rocket Hazmat 2012-04-04 23:51
new T1()
doesn't return a function, it returns a plain object - RobG 2012-04-04 23:55
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.
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.
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.