2
<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標籤,請問您能解釋一下您的問題嗎? – nana
爲什麼JavaScript在花括號之後放置了分號。通常大括號出現在句子結尾,我的第二個問題是 – swati16
爲什麼JavaScript在大括號後面放了分號。通常花括號在句子結束後出現,而我的第二個問題是在創建Cat對象之後將執行下一行。 – swati16