<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();
执行。