如何修复对象“undefined”

时间:2017-08-20 04:24:55

标签: javascript object

请告诉我如何解决,答案是“对象未定义”。

var dog = {
name : "Fido",
weight : 20.2,
age : 4,
breed : "mixed",
activity : "fetch balls"
};
var bark;
if (dog.weight > 20) {
bark = "WOOF WOOF";
} else {
bark = "woof woof";
}

var speak = dog.name + " says " + dog.bark + " when he wants to " +     dog.activity;
console.log(speak);

1 个答案:

答案 0 :(得分:0)

如果要将其设置为bark = "..."对象的属性,请将其设置为:

,而不是设置dog
dog.bark = "WOOF WOOF";



var dog = {
  name: "Fido",
  weight: 20.2,
  age: 4,
  breed: "mixed",
  activity: "fetch balls"
};

if (dog.weight > 20) {
  dog.bark = "WOOF WOOF";
} else {
  dog.bark = "woof woof";
}

var speak = dog.name + " says " + dog.bark + " when he wants to " + dog.activity;
console.log(speak);