0

I came across following scenario:

When I call function normally it can access contextObj

contextObj = {a: 22, b:55}
function user(){  console.log(this.contextObj.a)} 
user()

output = 22

but if I try following it gives me error undefined

contextObj = {a: 22, b:55}
function user(){  console.log(this.contextObj.a)} 
let obj = new user()

Can someone help me understand 'this'? is it because of keyword new

Thanks in advance :)

Shah_
  • 1
  • Yes, [`new`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new) instantiates a different object for `this`. – VLAZ Mar 26 '21 at 05:45

0 Answers0