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").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With