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"}
私はjavascriptとここに投稿するのが初めてです。
私の質問は、元の MyClass インスタンス化で「this.b」が変更されないのはなぜですか? ここで、javascript にブロック スコープがない (関数スコープのみ) を読みました。それが理由なら、なぜ「this.b」を「未定義」として扱わず、「now me B」に設定しないのでしょうか?
ありがとう!