function MyClass() {
this.a = "me a";
this.b = "me b";
};
MyClass.prototype.changeB = function(callback) {
this.a = "now me A";
doAnotherFunction(function(err, data) {
this.b = "now me B";
callback(null, this.b);});
};
function doAnotherFunction(callback) {
callback(null, null);
};
main();
function main() {
var myclass = new MyClass();
myclass.changeB(function(err, data) {
console.log("B: " + myclass.b + ", data: " + data);});
console.log(JSON.stringify(myclass));
}
When this runs:
B: me b, data: now me B
{"a":"now me A","b":"me b"}
Go easy on me, I'm new to javascript and to posting here.
My question is why does 'this.b' not get changed in the original MyClass instantiation? I've read in here where javascript does not have block scope (only function scope). If that was the reason, then why wouldn't it treat 'this.b' as 'undefined' where it sets it to "now me B"?
Thanks!