function MyFunc()
{
var msg = 'hello';
this.speak = function() {
console.log(msg);
}
}
var obj = new MyFunc();
obj.speak();
The weird thing about the above code is that it works. Yet 'msg' is not part of the object. So how is 'msg' kept in memory. I would more understand if I did:
this.msg = 'hello';
console.log(this.msg);
because then it's a part of the instantiated object. but how are regular variables like 'var msg' getting saved with this object?
edit: this question is not the same as closure because this is a specific use case.