English 中文(简体)
JavaScript的原型和遗产继承不能如我所预期的那样运作
原标题:Prototype and inheritance in JavaScript not functioning as I expect

我一直在学习 JavaScript 的课程, 使用原型, 最后如何继承。

根据我的理解,以下内容应:

  1. Alert "John" due to myInstance.getIt(); being called
  2. Alert "Jack" due to myInheritedInstance.getIt(); being called
  3. myInheritedInstance.getParent(); has been assigned to .getIt() in MyClass
  4. This should then alert "John" when myInheritedInstance.getParent(); is called.

相反,实际发生的情况是:

  1. Alert "John"
  2. Alert Blank
  3. Alert "Jack"

我有一种感觉,我做了一些蠢事 或者误解了一个基本概念, 所以,如果有任何帮助,都会感激不尽。

var MyClass = function() { };

MyClass.prototype.constructor = MyClass;
MyClass.prototype.name = "John";
MyClass.prototype.getIt = function () { alert(this.name); };

var myInstance = new MyClass();
myInstance.getIt();

//Now inheritance

var MyInheritedClass = function () { };
MyInheritedClass.prototype = new MyClass;
MyInheritedClass.prototype.constructor = MyInheritedClass;
MyInheritedClass.prototype.name = "Jack";
MyInheritedClass.prototype.getIt = function () { alert(this.name); };
MyInheritedClass.prototype.getItParent = MyClass.prototype.getIt.call(this);

var myInheritedInstance = new MyInheritedClass();
myInheritedInstance.getIt();
myInheritedInstance.getItParent();
最佳回答

罪魁祸首是:

MyInheritedClass.prototype.getItParent = MyClass.prototype.getIt.call(this);

.call call a 函数,而不是 return one。 这将引起两个问题: 它会事先叫它, 并返回一些无法调用的东西( 您在控制台中会有一个错误) 。 您需要做 :

MyInheritedClass.prototype.getItParent = function() {
    alert(Object.getPrototypeOf(Object.getPrototypeOf(this)).name);
};

问题在于 name 不再可以通过 this 进入,因为它已被继承的类别所覆盖。 要获得原始类的 name , 您必须两次登上原型链条 : insepherited exience - & gt; 继承的原型 - & gt; 原原型

线条线

MyClass.prototype.constructor = MyClass;

此处不需要。 如果您覆盖 prototype , 则需要恢复 < code> constructor , 因为在此情况下, < code> constructor 会丢失。 因此, 在您的情况中, 它只对继承的类有必要 。

还有,线线线

MyInheritedClass.prototype.getIt = function () { alert(this.name); };

多余, 与您继承的 < code> MyClasss. prototype. get it - 相同 。

注意JavaScript没有真正的“阶级”,

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签