I'm learning Javascript and I cannot understand why this code works:
function getObj()
{
var objAddress =
{
address: "Client Address",
getAddress: function() {
return this.address;
},
setAddress: function(newAddress)
{
this.address = newAddress;
}
};
var objClient =
{
name: "Client name",
getAddress: function()
{
return objAddress.getAddress();
},
setAddress: function(newAddress) {
objAddress.setAddress(newAddress);
}
};
return objClient;
}
gObj = getObj();
console.log(gObj.getAddress()); // Will print "Client Address"
gObj.setAddress("xpto");
console.log(gObj.getAddress()); // Will print "xpto"
I thought it would not work since getAddress() calls another method of an object that should not exist after leaving the function. But, as this is working, I presume that the object objAddress still exists even after quiting the getObj function.
Outside the function, how can the gObj.getAddress() work?

getAddress()calls doesn't exist?