を指していません。期待通りの方法が働くアップデート失敗 - JavaScriptは、「これは」私は人の人の最初の取得または設定しますコンストラクタ、最後の、そして完全な名前を持っている権利オブジェクト
var Person = function(firstAndLast) {
var self = this;
this.getFirstName = function(){
var first = self.firstName || (function(){
return firstAndLast.split(' ')[0];
})();
return first;
};
this.getLastName = function(){
var last = self.lastName || (function(){
return firstAndLast.split(' ')[1];
})();
return last;
};
this.getFullName = function(){
var full = self.fullName || (function(){
var first = firstAndLast.split(' ')[0];
var last = firstAndLast.split(' ')[1];
return first + " " + last;
})();
return full;
};
this.setFirstName = function(first){
self.firstName = first;
console.log('first name is now: ' + self.firstName);
};
this.setLastName = function(last){
self.lastName = last;
console.log('last name is now: ' + self.lastName);
};
this.setFullName = function(firstAndLast){
self.fullName = firstAndLast;
console.log('full name is now: ' + self.fullName);
};
};
取得...
var claude = new Person('Claude Shannon');
claude.getFullName();
しかし、なぜしないの次の作品?
claude.setFirstName('james');
claude.getFullName(); // "Claude Shannon"
(もちろん、私は「ジェームズ・シャノン
あなたは 'this'と' self'を場所全体に混ぜています。あなたのロジックを合理化したいかもしれません。 –
@FabianKlötzl申し訳ありませんが、投稿後に気づいただけです。私はそれを修正しようとしましたが、私はまだ私が期待した動作を取得していないようです。 –