2014-09-19 50 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; 
} 
}; 
相关问题