Javascript 原型继承的怪异

发布于 2024-12-09 12:25:09 字数 682 浏览 1 评论 0原文

我正在尝试完成一些 javascript 继承示例,但我遇到了这个问题:

function Animal(){}
Animal.prototype.type = "animal";
Animal.prototype.speak = function(){ console.log( "I'm a " + this.type + 
    ". I can't really talk ;)" ); }

function Dog(){}

function F(){}
F.prototype = Animal.prototype;

Dog.prototype = new F();
Dog.prototype.constructor = Dog;
Dog.prototype.type = "Dog";
Dog._super = Animal.prototype;
Dog.woof = function(){ console.log( "Woof!" ); _super.speak(); }

var rover = new Dog();
rover.woof();

我得到了这个,但我不知道为什么:

TypeError: Object #<Dog> has no method 'woof'

我知道我可以将未找到的方法放入构造函数中,但我正在尝试通过原型修改来做到这一点。我在这里做错了什么?

I'm trying to work through some javascript inheritance examples and I hit a wall with this one:

function Animal(){}
Animal.prototype.type = "animal";
Animal.prototype.speak = function(){ console.log( "I'm a " + this.type + 
    ". I can't really talk ;)" ); }

function Dog(){}

function F(){}
F.prototype = Animal.prototype;

Dog.prototype = new F();
Dog.prototype.constructor = Dog;
Dog.prototype.type = "Dog";
Dog._super = Animal.prototype;
Dog.woof = function(){ console.log( "Woof!" ); _super.speak(); }

var rover = new Dog();
rover.woof();

I am getting this and I have no idea why:

TypeError: Object #<Dog> has no method 'woof'

I know I can put the not-found method into the constructor function, but I am trying to do this with prototype modification. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淤浪 2024-12-16 12:25:09

更改:

Dog._super = Animal.prototype;
Dog.woof = function(){ console.log( "Woof!" ); _super.speak(); }

至:

// Dog.prototype._super = Animal.prototype; <- you can remove this line
Dog.prototype.woof = function(){ console.log( "Woof!" ); this.speak(); }

Change:

Dog._super = Animal.prototype;
Dog.woof = function(){ console.log( "Woof!" ); _super.speak(); }

To:

// Dog.prototype._super = Animal.prototype; <- you can remove this line
Dog.prototype.woof = function(){ console.log( "Woof!" ); this.speak(); }
悸初 2024-12-16 12:25:09

Dog 伪类定义的最后一个字符串是错误的。应该是

Dog.prototype.woof = function(){ console.log( "Woof!" ); Dog._super.speak.call(this); }
  1. 您应该将方法 woof 定义为 Dog 原型的属性。
  2. _super 只能作为 Dog 构造函数的属性使用。
  3. 您应该在当前实例的上下文中调用父类的方法。

The last string of the Dog pseudo-class definition is wrong. It should be

Dog.prototype.woof = function(){ console.log( "Woof!" ); Dog._super.speak.call(this); }
  1. You should define method woof as the property of the Dog's prototype.
  2. _super is available only as the property of the Dog constructor.
  3. You should call the methods of the parent class in context of the current instance.
梦明 2024-12-16 12:25:09

所以你的 woof 方法实际上是一个静态方法(如果你来自 java。基本上,它挂在 Dog 函数上,并且可以在没有 Dog 实例的情况下访问。即:Dog.woof())

让它工作对于狗的实例,您需要确保它是原型定义(同样,用 Java 类比,实际上是实例方法定义)。正如 qwertymik 所说,

Dog.prototype.woof = function(){ console.log( "Woof!" ); this.speak(); }

那么你就可以做到

var foo = new Dog();
foo.woof();

So your woof method is actually effectively a static method (If you're coming from java. Basically, it's hanging off the Dog function, and can be accessed without an instance of Dog. ie: Dog.woof())

To get it working with an instance of a dog, you want to make sure it's a prototype definition (again, with a Java analogy, effectively a instance method definition). As qwertymik said,

Dog.prototype.woof = function(){ console.log( "Woof!" ); this.speak(); }

Then you'll be able to do

var foo = new Dog();
foo.woof();
温柔少女心 2024-12-16 12:25:09

也许你的意思是这样做:

Dog.prototype._super = Animal.prototype;
Dog.prototype.woof = function(){ console.log( "Woof!" ); this._super.speak(); }

Maybe you mean to do this:

Dog.prototype._super = Animal.prototype;
Dog.prototype.woof = function(){ console.log( "Woof!" ); this._super.speak(); }
柠檬心 2024-12-16 12:25:09
//Rewrote the code to make it work

function Animal(){}
Animal.prototype.type = "animal";
Animal.prototype.speak = function(){ console.log( "I'm a " + this.type + 
    ". I can't really talk ;)" ); }

function Dog(){}
function F(){}

F.prototype = Object.create(Animal.prototype); //F inherits from animal
Dog.prototype = Object.create(F.prototype);    //Dog inherits from F

Dog.prototype.constructor = Dog; //resetting the constructor to Dog object
F.prototype.constructor=F;       // resetting the constrctor to F object 

Dog.prototype.type = "Dog";

Dog.prototype.woof = function(){ console.log( "Woof!" ); this.speak(); } //adding woof method to the prototype of dog

const rover = new Dog();

rover.woof();

// output
// Woof!
// I'm a Dog. I can't really talk ;)
//Rewrote the code to make it work

function Animal(){}
Animal.prototype.type = "animal";
Animal.prototype.speak = function(){ console.log( "I'm a " + this.type + 
    ". I can't really talk ;)" ); }

function Dog(){}
function F(){}

F.prototype = Object.create(Animal.prototype); //F inherits from animal
Dog.prototype = Object.create(F.prototype);    //Dog inherits from F

Dog.prototype.constructor = Dog; //resetting the constructor to Dog object
F.prototype.constructor=F;       // resetting the constrctor to F object 

Dog.prototype.type = "Dog";

Dog.prototype.woof = function(){ console.log( "Woof!" ); this.speak(); } //adding woof method to the prototype of dog

const rover = new Dog();

rover.woof();

// output
// Woof!
// I'm a Dog. I can't really talk ;)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文