2014-09-19 85 views
-2
function Human(name, currentMail, currentScore, newScore) { 
    this.name = name; 
    this.currentMail = currentMail; 
    this.currentScore = currentScore; 
    this.newScore = newScore; 
    this.changeScore = function (ns) { 
     if (ns != "") { 
      console.log(ns + "Is Your New Score"); 
      this.currentScore = ns; 
     } 
    }; 

    this.changeMail = function (cmail) { 
     if (cmail != this.currentMail) { 
      console.log(cmail + "Is Your New Mail"); 
      currrentMail = cmail 
     }; 
    }; 
} 
var ilan = new Human(); 
ilan = { 
    name: "Ilan Vachtel", 
    currentMail: "[email protected]", 
    currentScore: "85" 
}; 
console.log(ilan); 
ilan.newScore = "89"; 
ilan.changeScore("45"); 
console.log(ilan); 

此代碼運行我這個錯誤我不明白我做錯了什麼,請幫忙嗎? 它的一個開始,但是,不能明白爲什麼它不工作 感謝提前未捕獲TypeError:undefined不是功能匿名函數

+0

不要再將其初始化爲JavaScript對象。刪除此行'ilan = {name:「Ilan Vachtel」,currentMail:「[email protected]」,currentScore:「85」}; ' – andrex 2014-09-19 22:17:29

+2

@Ilan,在寫問題時請花一些時間,讓其他人花更少的時間幫助你。這次我會進行格式化,但下次要由您決定。 – Miki 2014-09-19 22:21:32

+0

Isthiscommenteasilyilyreadable?No?Thenyoumightconsiderspacesinyourcode,at leastastwhenaskingotherforhelp。 – 2014-09-19 22:21:45

回答

0

你設置ilan兩次,第一次給函數的一個實例,然後到一個對象

var ilan = new Human(); 

ilan = {name:"Ilan Vachtel",currentMail:"[email protected]",currentScore:"85"}; 

這兩項的設置的值爲ilan,最後一個是粘貼的值,它沒有changeScore屬性,因此在嘗試調用undefined函數時出錯。

2

這條線使用Human功能設置了一個對象:

var ilan = new Human(); 

但隨後該行扔掉該對象,並用它並沒有任何關係,與你在做什麼完全新的對象來替換它你Human功能:

ilan = {name:"Ilan Vachtel",currentMail:"[email protected]",currentScore:"85"}; 

因此,由於有無關Human,它沒有changeScore

鑑於Human是如何定義的,可以取代這兩條線以及與它們後面的ilan.newScore = "89";行:

var ilan = new Human("Ilan Vachtel", "[email protected]", "85", "89"); 

...除了有newScore參數的構造並沒有真正多大感。

下面是該代碼可能會通常被寫入(見註釋):

function Human(name, currentMail, currentScore){ 
    this.name = name; 
    this.currentMail = currentMail; 
    this.currentScore = currentScore; 
    // Having `newScore` here doesn't make any sense 
} 

Human.prototype.changeScore = function(ns) { 
    if (ns != "") { 
     console.log(ns+"Is Your New Score"); 
     this.currentScore=ns; 
    } 
}; 

Human.prototype.changeMail = function(cmail) { 
    if (cmail != this.currentMail) { 
     console.log(cmail + "Is Your New Mail"); 
     this.currentMail = cmail; // <== `this.currentMail`, not `currrentMail` 
    } // <== No ; here 
}; 

var ilan = new Human("Ilan Vachtel", "[email protected]", "85"); 
ilan.changeScore("89"); 
console.log(ilan); 
0

有幾件事情錯過這裏。首先,你的函數應該初始化的方式是關鍵字new,而不是一個普通的對象。

var ilan = new Human(); 
ilan = {name:"Ilan Vachtel",currentMail:"[email protected]",currentScore:"85"}; 

這是不正確的,因爲它將ilan分配爲對象。但是,ilan已經是一個從new Human構建的Function對象。人類有四個參數的構造函數中

function Human(name,currentMail,currentScore,newScore){ 

並且你可以通過這些參數,當你實例化對象的人利用這一點。

var ilan = new Human("Ilan Vachtel","[email protected]","85");//newScore will be undefined 
console.log(ilan);//now will show a fully constructed object 
ilan.newScore = "89"; 
ilan.changeScore("45"); 
console.log(ilan); 

使用this.changeScore = function當你造成一個匿名函數來創建的每個新的人類被創建時也請記住這一點。附加此功能的建議方式是通過如下原型:

//constructor function 
function Human(name, currentMail, currentScore, newScore){ 
this.name = name; 
this.currentMail = currentMail; 
this.currentScore = currentScore; 
this.newScore = newScore; 
} 

//prototype functions for Human 
Human.prototype.changeScore = function(ns){ 
if (ns != ""){ 
    console.log(ns+"Is Your New Score"); 
    this.currentScore = ns; 
} 
}; 

Human.prototype.changeMail = function(cmail){ 
if(cmail != this.currentMail){ 
    console.log(cmail + "Is Your New Mail"); 
    this.currrentMail = cmail; 
} 
}; 
相關問題