简体   繁体   English

JavaScript原型继承问题

[英]Javascript prototype inheritance issue

I'm having an issue with prototype inheritance and I can't find out why it's not working properly. 我在原型继承方面遇到问题,无法找出为什么它无法正常工作。

The snippet is this: 代码段是这样的:

function Field(newKey, val) {
    this.key = newKey || "";
    this.value = val || "";
}

Field.prototype.isEmpty = function() {
    return this.value === undefined || this.value === null || this.value === "";
};

function DoubleField(newKey, val) {
    this.base = Field;
    this.base(newKey, val);
}
DoubleField.prototype = new Field();
DoubleField.prototype.constructor = DoubleField;

function IntegerField(newKey, val) {
    this.base = DoubleField;
    this.base(newKey, val);
}
IntegerField.prototype = new DoubleField();
IntegerField.prototype.constructor = IntegerField;

var f = new Field('keyfield', 'valField');
var d = new DoubleField('keydouble', 'valDouble');
var i = new IntegerField('keyinteger');

var res = f.isEmtpy();

The call to f.isEmpty is failing? 对f.isEmpty的调用失败了吗? Why? 为什么? Calls to d.isEmpty or i.isEmpty work just fine as expected. 调用d.isEmpty或i.isEmpty可以正常工作。

I cannot realize what I'm doing wrong. 我不知道我在做什么错。 Any help would be much appreciated! 任何帮助将非常感激!

The error is in the last line of code: 错误在代码的最后一行:

var res = f.isEmtpy();//That's what you wrote

The correct is: 正确的是:

var res = f.isEmpty();

You haven't said how it's failing. 您还没有说这是怎么回事 As I noted in a comment, at the very least there's a typo: var res = f.isEmtpy(); 正如我在评论中指出的那样,至少有一个错字: var res = f.isEmtpy(); should be var res = f.isEmpty(); 应该是var res = f.isEmpty();

But beyond that (which I assume is just a typo in the question , not the code), note that this line in your derived constructors: 但除此之外,(我以为是刚在的问题 ,而不是代码一个错字),请注意,这条线在派生的构造函数:

this.base = DoubleField;

...will not work as you intend. ...将无法按预期工作。 Consider the case of IntegerField . 考虑IntegerField的情况。 You call new IntegerField and it sets this.base to DoubleField and calls it. 你叫new IntegerField并将其设置this.baseDoubleField并调用它。 But the DoubleField constructor then sets this.base to Field . 但是DoubleField构造函数然后将this.base设置为Field So you have an IntegerField instance with a base property pointing to Field , not DoubleField . 因此,您有一个IntegerField实例,该实例的base属性指向Field ,而不是DoubleField

You can't use instance properties to trace lineage. 您不能使用实例属性来跟踪血统。 It works for a parent/child situation, but you run into the "grandchild" problem described above. 它适用于父母/孩子的情况,但是您遇到了上述的“孙子”问题。

I recommend using one of the inheritance helper scripts out there. 我建议在那里使用一种继承帮助程序脚本。 There are several, including my own Lineage . 有几本,包括我自己的《 Lineage These scripts help you build inheritance hierarchies, handling the plumbing for you so you can focus on building whatever it is you're trying to build. 这些脚本可帮助您构建继承层次结构,为您处理管道,因此您可以专注于构建要构建的内容。 We only need them temporarily; 我们只是暂时需要它们。 JavaScript is getting syntactic sugar to help with this in the next version (but of course, it'll be years before we see widespread support of it, once it's actually finalized). JavaScript在下一个版本中正在获得语法糖来帮助解决此问题 (但是,一旦它最终确定,我们还需要很多年才能看到它的广泛支持)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM