Javascript Uncaught TypeError:undefined不是函数anonymos函数
这个代码运行我这个错误我不明白我做错了什么,请帮助? 这是一个开始,但我不明白为什么它不起作用Javascript Uncaught TypeError:undefined不是函数anonymos函数,javascript,Javascript,这个代码运行我这个错误我不明白我做错了什么,请帮助? 这是一个开始,但我不明白为什么它不起作用 感谢大家您设置了ilan两次,首先是函数实例,然后是对象 function Human(name, currentMail, currentScore, newScore) { this.name = name; this.currentMail = currentMail; this.currentScore = currentScore; this.newScore = n
感谢大家您设置了
ilan
两次,首先是函数实例,然后是对象
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: "ilanvac@gmail.com",
currentScore: "85"
};
console.log(ilan);
ilan.newScore = "89";
ilan.changeScore("45");
console.log(ilan);
这两个函数都设置了
ilan
的值,最后一个是粘滞的,并且它没有changeScore
属性,因此当您试图调用未定义的函数时会出现错误。此行使用Human
函数设置对象:
var ilan = new Human();
ilan = {name:"Ilan Vachtel",currentMail:"ilanvac@gmail.com",currentScore:"85"};
var ilan = new Human();
但是,这行代码扔掉了那个对象,并用一个全新的对象替换它,这个对象与您在Human
函数中所做的事情没有任何关系:
var ilan = new Human();
ilan = {name:"Ilan Vachtel",currentMail:"ilanvac@gmail.com",currentScore:"85"};
var ilan = new Human();
因此,由于这与人类
无关,因此它没有变更分数
根据Human
的定义,您可以替换这两行和ilan.newScore=“89”代码>紧随其后的一行:
ilan = {name:"Ilan Vachtel",currentMail:"ilanvac@gmail.com",currentScore:"85"};
…除了构造函数有一个newScore
参数没有多大意义之外
下面是该代码通常的编写方式(请参见注释):
功能人员(姓名、当前邮件、当前分数){
this.name=名称;
this.currentMail=currentMail;
this.currentScore=currentScore;
//这里有“新闻核心”没有任何意义
}
Human.prototype.changeScore=函数(ns){
如果(ns!=“”){
log(ns+“是您的新分数”);
此.currentScore=ns;
}
};
Human.prototype.changeMail=函数(cmail){
if(cmail!=此.currentMail){
log(cmail+“是您的新邮件”);
this.currentMail=cmail;//这里缺少一些东西。首先,初始化函数的方法是使用new
关键字,而不是普通对象
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", "ilanvac@gmail.com", "85");
ilan.changeScore("89");
console.log(ilan);
这是不正确的,因为它将ilan指定为一个对象。但是,ilan已经是一个由new Human
构造的函数对象。Human在其构造函数中有四个参数
var ilan = new Human();
ilan = {name:"Ilan Vachtel",currentMail:"ilanvac@gmail.com",currentScore:"85"};
当你实例化你的人类对象时,你可以通过传递这些参数来利用这一点
function Human(name,currentMail,currentScore,newScore){
还请记住,当使用this.changeScore=function
时,每次创建新的人员时,您都会创建一个匿名函数。附加此函数的建议方法是通过如下原型:
var ilan = new Human("Ilan Vachtel","ilanvac@gmail.com","85");//newScore will be undefined
console.log(ilan);//now will show a fully constructed object
ilan.newScore = "89";
ilan.changeScore("45");
console.log(ilan);
不要再次将其初始化为javascript对象。删除这一行`ilan={name:“ilan Vachtel”,currentMail:“ilanvac@gmail.com,currentScore:“85”}“@Ilan,请在写问题时花点时间,这样其他人可以少花点时间帮助你。这次我会格式化,但下次由你决定。这篇评论容易阅读吗?不?那么你可以考虑在你的代码中使用空格,至少在使用其他人的密码时。@T.J.Crowder抱歉,我是通过第一篇文章的评论队列得到这个问题的。评论,然后我就可以开始了。”我没有时间一下子完成所有的事情。虽然我不太理解“被锁定”的情况?@Miki:这是编辑审批系统中令人恼火的一个方面。如果我选择“批准”而不是“改进”,那么在整个编辑被批准之前,我无法编辑。