プロトタイプを使用するメソッドとプロトタイプを使用しないメソッドのオーバーライドの違いは何ですか。検討:
例 1:
function Animal() {
this.sleep = function () {
alert("animal sleeping");
};
this.eat = function () {
alert("animal eating");
};
}
function Dog() {
this.eat = function () {
alert("Dog eating");
};
}
Dog.prototype = new Animal;
var dog = new Dog;
dog.eat();
例 2:
function Animal() { }
function Dog() { }
Animal.prototype.sleep = function () {
alert("animal sleeping");
};
Animal.prototype.eat = function () {
alert("animal eating");
};
Dog.prototype = new Animal;
Dog.prototype.eat = function () {
alert("Dog eating");
};
var dog = new Dog;
dog.eat();
Dog
どちらの例も、クラスがクラスの eat メソッドをオーバーライドしているのと同じ効果を生み出すと思いますAnimal
。それとも、何か違うことが起こっていますか?