関数実行後に変数fullNameをコンソールで操作しようとしましたが、値が変更されず、コンソールのデフォルト値Not Set
、なぜですか?インスタンス関数の変数は変更されていませんか?
function Test() {
this.clientData = {
fullName : "Not Set",
setUserName: function (firstName, lastName) {
this.fullName = firstName + " " + lastName;
},
getUserInput2: function (firstName, lastName, callback) {
callback(firstName, lastName);
}
};
this.getUserInput1 = function (firstName, lastName, callback) {
callback(firstName, lastName);
};
}
var test = new Test();
test.getUserInput1("A1", "B1", test.clientData.setUserName);
console.log("test.clientData.fullName : " + test.clientData.fullName);//Not Set
//expected => A1 B1
test.clientData.getUserInput2("A2", "B2", test.clientData.setUserName);
console.log("test.clientData.fullName : " + test.clientData.fullName);//Not Set
//expected => A2 B2
おそらくコールバック関数*の[* 'this']の複製です(http://stackoverflow.com/questions/14561723/this-in-callback-functions)。また、あなたは[* "どのように"この "キーワードの作品は?*](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work/3127440#3127440)と[ * MDN:this *](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)。 – RobG