function Dog(name, class, breed) {
this.name = name;
this.class = class;
this.breed = breed;
}
var harry= new Dog("harry", 15, "indian");
var tom = new Dog("tom", 16, "american");
document.write(tom.name);
这不会在浏览器中打印tom ..为什么?????
答案 0 :(得分:1)
class
是保留字,因此您不能将其用作属性名称。您可以将其更改为其他名称,例如Class
要克服这个问题。
function Dog(name, Class, breed) {
this.name = name;
this.Class = Class;
this.breed = breed;
}
答案 1 :(得分:1)
这是因为您使用了class
这个词,但class
是一个保留词。所以用其他东西替换class
。这就是你的代码应该是这样的。我用cls替换了class
<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
function Dog(name, cls, breed) {
this.name = name;
this.cls = cls;
this.breed = breed;
}
var harry= new Dog("harry", 15, "indian");
var tom = new Dog("tom", 16, "american");
document.write(tom.name);
//-->
</script>
</body>
</html>