2017-03-09 1 views
-1

を指していません。期待通りの方法が働くアップデート失敗 - 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" 

(もちろん、私は「ジェームズ・シャノン

+0

あなたは 'this'と' self'を場所全体に混ぜています。あなたのロジックを合理化したいかもしれません。 –

+0

@FabianKlötzl申し訳ありませんが、投稿後に気づ​​いただけです。私はそれを修正しようとしましたが、私はまだ私が期待した動作を取得していないようです。 –

答えて

1

シンプルに、実際には新しいフルネーム(setFirstNamesetLastNameで設定された2つのプロパティで構成されています)を返すことはありません。あなたはコンストラクタから1つを返します。あなたのgetFullName機能で

this.getFullName = function(){ 
    var full = this.fullName || (function(){ 
    var first = firstAndLast.split(' ')[0]; 
    var last = firstAndLast.split(' ')[1]; 
    return first + " " + last; 
    })(); 
    return full; 
}; 

this.getFullName = function(){ 
    return this.getFirstName() + " " + this.getLastName() 
}; 

var Person = function(firstAndLast) { 
 
    var self = this; 
 
    this.getFirstName = function(){ 
 
     var first = this.firstName || (function(){ 
 
     return firstAndLast.split(' ')[0]; 
 

 
     })(); 
 
     return first; 
 
    }; 
 
    this.getLastName = function(){ 
 
     var last = this.lastName || (function(){ 
 
     return firstAndLast.split(' ')[1]; 
 

 
     })(); 
 
     return last; 
 
    }; 
 
    this.getFullName = function(){ 
 
     return this.getFirstName() + " " + this.getLastName() 
 
    }; 
 
    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'); 
 
console.log(claude.getFullName()); 
 

 
claude.setFirstName('James'); 
 
console.log(claude.getFullName()); // "Claude Shannon"

0

を期待していた私は、プロトタイプにすべてのメソッドを置くために助言とちょうどどこでもこれを使用すると思います。

しかし、問題はgetFullName()が見えることですthis.fullNameプロパティで this.firstNameのみをJamesに設定するsetFirstName()を使用したので、getFullNameはthis.fullNameが未定義であるため、コンストラクタで使用されるパラメータ、つまり 'Claude Shannon'を見直し続けます。

0

コンストラクタに渡された最初の引数からフルネームを読み込んでいます。これは決して更新されません。プロパティから読み込む必要があります。姓または名字を取得するときは、動的プロパティから読み込みますが、フルネームについては、常に最初のコンストラクタargから読み込みます。結構ですthis -

さらに、selfに書き込みする理由はありません。

私は他のいくつかの変更を行いました。 Fiddle

var Person = function(first, last) { 
    //var self = this; <-- unnecessary 
    this.getFirstName = function(){ 
     var first = this.firstName || (function(){ 
     return firstAndLast.split(' ')[0]; 

     })(); 
     return first; 
    }; 
    this.getLastName = function(){ 
     var last = this.lastName || (function(){ 
     return firstAndLast.split(' ')[1]; 

     })(); 
     return last; 
    }; 
    this.getFullName = function(){ 
     //much simpler than before - just concatenate properties 
     return this.firstName+' '+this.lastName; 
    }; 
    this.setFirstName = function(first){ 
     this.firstName = first; 
     alert('first name is now: ' + this.firstName); 
    }; 
    this.setLastName = function(last){ 
     this.lastName = last; 
     console.log('last name is now: ' + this.lastName); 
    }; 
    this.setFullName = function(firstAndLast){ 
     this.fullName = firstAndLast; 
     console.log('full name is now: ' + this.fullName); 
    }; 
    this.setFirstName(first); //set initial first name passed to construc 
    this.setLastName(last); //" " last " " " " 
}; 

var claude = new Person( 'Claude'、 'Shannon'); //注記、2つの別個のargs claude.setFirstName( 'James'); アラート(claude.getFullName()); // "James Shannon"

+0

私は仕事をするために特定の種類のデータを与えられました.2つの引数を持つオプションはありません –

+0

あなたは大歓迎です。 – Utkanos

0

には、最初に名前をチェックされていません。フルネームが存在するかどうかだけを確認しています。コード内のコンテキストを修正しても、ロジックが原因で問題に直面します。

this.getFullName = function(){ 
    var full = this.fullName || (function(){ 
    var first = firstAndLast.split(' ')[0]; 
    var last = firstAndLast.split(' ')[1]; 
    return first + " " + last; 
    })(); 
    return full; 
}; 
関連する問題