【发布时间】:2012-09-13 04:06:26
【问题描述】:
<html><head><script>
function Pet(){ // Base Class
var owner = "Mrs. Jones";
var gender = undefined;
this.setOwner = function(who) { owner=who; }; //1
this.getOwner = function(){ return owner; }
this.setGender = function(sex) { gender=sex; }
this.getGender = function(){ return gender; }
}
function Cat(){} //subclass constructor
Cat.prototype = new Pet();
Cat.prototype.constructor=Cat;
Cat.prototype.speak=function speak(){
return("Meow"); //2
}; //3
function Dog(){}; //4
Dog.prototype= new Pet();
Dog.prototype.constructor=Dog;
Dog.prototype.speak = function speak(){
return("Woof");
}; //5
</script></head>
<body><script>
var cat = new Cat;
var dog = new Dog;
cat.setOwner("John Doe");
cat.setGender("Female");
dog.setGender("Male");
document.write(
"<br>The cat is a "+ cat.getGender()+ " owned by "
+ cat.getOwner() +" and it says " + cat.speak());
document.write(
"<br>The dog is a "+ dog.getGender() + " "
+ " owned by " + dog.getOwner() + " and it says " + dog.speak());
</script></body>
- 为什么在上面代码中标记为
//1、//2、//3、//4和//5的行的右花括号后面有分号? - 何时会执行
Cat.prototype = new Pet();和Dog.prototype = new Pet();。
【问题讨论】:
-
我重新格式化了您的代码并添加了缺少的 html 标签,您能否再解释一下您的问题?
-
为什么 JavaScript 在大括号后面加上分号。通常大括号出现在句末之后,我的第二个问题是
-
为什么 JavaScript 在大括号后面加上分号。通常花括号出现在句末之后,我的第二个问题是在创建 Cat 对象之后将执行下一行。