2016-06-16 18 views
0

私はJavaスクリプトで単純なプロトタイプの継承を行っていますが、ここでは2つのパラメータtitleを渡していますが、真ならtrueを返しますが、 "Angular Yes"の結果を得ていますが、代わりに「NaNを無」の結果になっていない「HTMLしても、」私はあなたのprint方法の浮遊プラス記号を持っているHTMLなしプロトタイプ継承

//Base Class creation 
     var job = function() { 
      this.pays = true 
     } 
     job.prototype.print = function() { 

      console.log(this.pays ? 'yse' : 'no') 
     } 
     //Sub Class creation 
     var techjob = function (title, pays) { 
      job.call(this); 
      this.title = title; 
      this.pays = pays; 
     } 

     techjob.prototype = Object.create(job.prototype); 
     techjob.prototype.constructor = techjob; 

     job.prototype.print = function() { 
      console.log(this.pays ? this.title + " " + 'yes' : + this.title + " " + 'no') 
     } 

     var subject1 = new techjob("Angular", true); 
     var subject2 = new techjob("HTMl", false); 

     console.log(subject1.print()) 
     console.log(subject2.print()) 

     Result 

     Angular yes 
     NaN no 
+0

:これを試してみてください。私は2番目のものが代わりに 'techjob.prototype.print'に割り当てられることを意図していると考えています。 –

+0

注:JavaScriptの*圧倒的な*慣習は、あなたの 'job'や' techjob'のような、 'Job'と' TechJob'のような、最初にキャップされるコンストラクタ関数です。明らかにコード内で好きなことをすることはできますが、助けを求めるときは標準的な規則に従うのが便利です。 –

答えて

0

を印刷したいんしています。 Schlausは(this.title` `前に` + `余分な)を指摘し、あなたがjob.prototype.print``に2つの異なる機能を割り当てていることを除けば、タイプミスから

job.prototype.print = function() { 
    console.log(this.pays ? this.title + " " + 'yes' : this.title + " " + 'no') 
}