Javascript中的构造函数是';行不通
我需要提示输入5个属性,然后获取测试分数并获得测试1、2和3的平均值,然后显示名称和平均值。我无法使用它来显示或运行该函数。我的代码有什么问题Javascript中的构造函数是';行不通,javascript,function,prompt,function-constructor,Javascript,Function,Prompt,Function Constructor,我需要提示输入5个属性,然后获取测试分数并获得测试1、2和3的平均值,然后显示名称和平均值。我无法使用它来显示或运行该函数。我的代码有什么问题 function Student(_firstName, _lastName, _t1, _t2, _t2){ this.firstName = _firstName ; this.lastName = _lastName ; this.test1 = _t1 ; this.test2 = _t2 ; this.
function Student(_firstName, _lastName, _t1, _t2, _t2){
this.firstName = _firstName ;
this.lastName = _lastName ;
this.test1 = _t1 ;
this.test2 = _t2 ;
this.test3 = _t3 ;
this.fullName = function() { return this.firstName + " " + this.lastName } ;
this.calcAverage = function() { return (this.test1 + this.test2 + this.test3) / 3 } ;
}
var name1 = prompt("Enter the first name:") ;
var name2 = prompt("Enter the last name:") ;
var te1 = parseInt(prompt("Enter the first test score:")) ;
var te2 = parseInt(prompt("Enter the second test score:")) ;
var te3 = parseInt(prompt("Enter the third test score:")) ;
var person = new Student(name1, name2, te1, te2, te3) ;
document.write(+name1+ " " +name2+ " " + person.calcAverage() +) ;
工作代码
function Student(_firstName, _lastName, _t1, _t2, _t3) {
this.firstName = _firstName;
this.lastName = _lastName;
this.test1 = _t1;
this.test2 = _t2;
this.test3 = _t3;
this.fullName = function () {
return this.firstName + " " + this.lastName
};
this.calcAverage = function () {
return (this.test1 + this.test2 + this.test3) / 3;
};
}
var name1 = prompt("Enter the first name:");
var name2 = prompt("Enter the last name:");
var te1 = parseInt(prompt("Enter the first test score:"));
var te2 = parseInt(prompt("Enter the second test score:"));
var te3 = parseInt(prompt("Enter the third test score:"));
var person = new Student(name1, name2, te1, te2, te3);
document.write(+name1 + " " + name2 + " " + person.calcAverage());
修正你的打字错误:
function Student(_firstName, _lastName, _t1, _t2, _t3) {
^^ _t3, not _t2
document.write(+ name1 + " " + name2 + " " + person.calcAverage() +);
^^ remove + ^^ remove +
这有什么用?控制台中有错误吗?有很多错误。参数名称为双精度,document.write具有不必要的“+”符号。