我刚开始使用JOP上的OOP。我是编程世界的新手。你能帮我解决下面的代码吗?我的文本编辑器在'else'块上显示语法错误。
function Dog(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function () {
if (this.weight > 25) alert(this.name + " says Woof")
} else {
alert(this.name + " says Poof");
}
}
var fido = new Dog("Fido", "Mixed", 38);
fido.bark();
答案 0 :(得分:2)
{
之后的if (this.weight > 25)
以及.
与fido
之间的bark();
function Dog(name, breed, weight){
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function(){
if (this.weight > 25){
alert(this.name + " says Woof")
} else {
alert(this.name + " says Poof");
}
}
}
var fido = new Dog("Fido", "Mixed", 38);
fido.bark();
答案 1 :(得分:1)
if (this.weight > 25)
alert(this.name + " says Woof")
}
else {
alert(this.name + " says Poof");
}
你不打开if {
答案 2 :(得分:1)
function Dog(name, breed, weight){
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function(){
if (this.weight > 25){
alert(this.name + " says Woof")
}
else {
alert(this.name + " says Poof");
}
}
}
var fido = new Dog("Fido", "Mixed", 38);
fido bark();
答案 3 :(得分:1)
试试这个
function Dog(name, breed, weight){
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function(){
if (this.weight > 25){
alert(this.name + " says Woof");
}
else {
alert(this.name + " says Poof");
}
};
}
var fido = new Dog("Fido", "Mixed", 38);
fido.bark();
答案 4 :(得分:0)
if (this.weight > 25)
alert(this.name + " says Woof")
}
出了问题,你错过了{
。它应该是:
if (this.weight > 25)
{
alert(this.name + " says Woof")
}