const user1 = {
name: 'Sherlock Holmes',
address:{
street:'Baker street',
number:"221B",
},
sayMyAddress:function (){
console.log(`My address is ${this.address}`)
console.log(this.address)
}
}
user1.sayMyAddress()
The result:
My address is [object Object]
{ street: 'Baker street', number: '221B' }
Why the this keyword shows the address value in the second case and doesn't in the first? the scope in this case isn't in the object user?
this. The same result can be observed withconsole.log({})andconsole.log(`${{}}`)[object Object], you can get the same result if doconsole.log(this.address.toString()). In the second case you set the first parameter to the console of the type object, so depending of the WB you would get the representation of the objecttoString()function inaddressobject, then the 1stconsole.log()would be able to print out the correct answer w.r.t thetoString()defined.