1
function test(){
  this.name = 'My function';
}
let a = test();
let b = new test(); // Is affect on performance,memory?

Call the function directly and call via constructor. What is the diffrence between?

Keval-IW
  • 21
  • 3
  • log the value of `this` in both calls and observe the difference. – Yousaf Jan 17 '22 at 05:34
  • 1
    It is not a matter of performance. They are fundamentally different. Just `console.log(a,b)`, you will see `a` is undefined while `b` is the constructed object. `test()` invokes a function and returns the return value of the function. `new text()` create an object, like creating an instance of a class. – Ricky Mo Jan 17 '22 at 05:36
  • a will return undefined. and if you try calling a.name it will throw an uncaught typeerror. So b is the way to go for this example – Kelvin Chong Jan 17 '22 at 05:37

0 Answers0