Le code suivant provient de JavaScript par exemple Deuxième édition, cela fonctionne bien. Je ne comprends pas complètement l'utilisation constructeur, après avoir commenté la ligne Dog.prototype.constructor = Dog et Cat.prototype.constructor = Cat Je trouve que ça marche aussi et j'obtiens le même résultat, pourquoi? Merci!Le programme peut toujours fonctionner normalement même si je commente la ligne Dog.prototype.constructor = Dog, pourquoi?
<html>
<head><title>Creating a subclass</title>
<script type="text/javascript">
function Pet(){ // Base Class
var owner = "Mrs. Jones";
var gender = undefined;
this.setOwner = function(who) { owner=who;};
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");
};
function Dog(){};//subclass constructor
Dog.prototype= new Pet();
Dog.prototype.constructor=Dog;
Dog.prototype.speak = function speak(){
return("Woof");
};
</script>
</head>
<body><big>
<script>
var cat = new Cat;
var dog = new Dog;
cat.setOwner("John Doe");
cat.setGender("Female");
dog.setGender("Male");
document.write("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>
</big>
</body>
</html>
parce que les ordinateurs ont évolué à un point où vous n'avez plus le contrôle total sur eux et ils ont l'intention de conquérir le mot avec l'aide de chatons du futur – ajax333221