Function B (a) {this.a = a;} an interview question about constructor instantiation

function B(a) {  
    this.a = a;  
}  

console.log(new B());   // B{ a:undefined }

output new B (), this is not the output is the return value of the B function? If there is no return value, the result should be undefined, why is the output of the function itself?

May.22,2021

I tried and did not output the function itself.


new operator
these are all basic things
using the new operator is definitely not the same as the usual function call


.

conclusion: the output is not the function itself, but the object of type B

you need to know what new does:
(1) create a new object;
(2) assign the scope of constructor B to the new object (so this points to the new object)
(3) execute the code in the constructor (add properties to the new object);
(4) return the new object.

Menu