【问题标题】:Javascript accessing class variable from an inherited class method defined in parentJavascript从父类中定义的继承类方法访问类变量
【发布时间】:2013-10-11 02:49:34
【问题描述】:

嗯,我是原型编程/设计的新手。 我很乐意为您提供帮助。

问题是为什么我在“find”方法中的“this.__proto__instances”返回“undefined”?

如果我的方法是错误的,请原谅我,我很高兴知道调用类方法以在类变量数组中查找元素的正确方法,而无需为每个孩子定义方法。

详细问题在下面的代码中作为 cmets 进行了阐述。

谢谢。

function Attribute(name,type){
    //some members definition, including uid, name, and type
};

Attribute.prototype.find=function(uid){
    var found_attr=false;
    this.__proto__.instances.forEach(function(attr){ 
        if (attr.uid == uid) found_attr=attr;           
    }); 
    return found_attr;
};

this.__proto__.instances.forEach(function(attr){ 上面是错误的行。日志说“无法为未定义的每个调用方法”

function ReferenceAttribute(arg_hash){
    Attribute.call(this,arg_hash.name,arg_hash.type); 
    //some members definition
    this.pushInstance(this); 
};

this.pushInstance(this); 将此实例推送到正常工作的 ReferenceAttribute.prototype.instances

ReferenceAttribute.prototype=new Attribute(); 

ReferenceAttribute 通过原型链方法继承 Attribute

ReferenceAttribute.prototype.instances=new Array(); 

上面的行声明了包含所有引用属性实例的数组。 对于 ReferenceAttribute 的每个新对象,它将被推送到这个数组中,在方法 pushInstance() 中完成。 推送总是成功的,我通过控制台日志检查了它们。该数组确实包含 ReferenceAtribute 实例

function ActiveAttribute(arg_hash){
    Attribute.call(this,arg_hash.name,arg_hash.type);
    //some members definition
    this.pushInstance(this); 
};

ActiveAttribute.prototype=new Attribute(); 
ActiveAttribute.prototype.instances=new Array(); 

在程序中使用它

var ref_attr=ReferenceAttribute.prototype.find("a uid"); 

给出的错误是它不能调用未定义的 forEach 方法。 它可以调用方法find,因此可以很好地继承。但是我猜find方法定义中的“this._proto_instances”是错误的。

编辑:

Attribute.prototype.pushInstance=function(my_attribute){    
    this.__proto__.instances.push(my_attribute);    
}; 

此功能有效。虽然实例数组由 ActiveAttribute 或 ReferenceAttribute 拥有,而不是 Attribute 本身,但此函数确实可以将其推送到数组。

【问题讨论】:

  • 谢谢。我会做一些编辑

标签: javascript oop prototype


【解决方案1】:

因为你这样做:

var ref_attr=ReferenceAttribute.prototype.find("a uid"); 

ReferenceAttribute.prototype 对象是从Attribute 构造函数创建的实例,Attribute.prototype 没有.instances 属性,也没有直接在对象上定义.instances 属性。

【讨论】:

  • 感谢您的回答。我很感激。我在代码中做了一些编辑。有一个 pushInstance() 方法定义为 Attribute 的方法。它在两个孩子的构造函数中调用。这种方法确实可以将实例推送到数组中。我确实注意到与“find()”的区别在于 pushInstance 是从子实例(对象)调用的,而 find() 不是从子对象调用的,而是在类上下文中调用的。您对此有何建议?
  • ReferenceAttribute 的实例而不是从构造函数的原型调用它,以便它到达该实例的数组。 (new ReferenceAttribute).find("a uid")
  • 这很有道理,真的。我会试试。谢谢。
【解决方案2】:

user2736012 有你的答案,所以只是评论:

__proto__ 属性不是标准化的,也不是所有正在使用的浏览器都支持的,所以不要使用它。此外,如果您想访问 Object 的 [[Prototype]] 的属性,请使用标准属性解析:

this.instances

如果要直接引用继承的方法,继承的意义何在?

【讨论】:

  • 感谢您的建议。 user2736012 的解决方案适用于我需要创建子属性的虚拟实例。关于您上面的问题,我的目的是让我不需要为每个孩子定义“查找”方法。假设我有很多继承属性的孩子,我怎么能只写一次那个方法?我的问题已经解决,但如果您有几分钟的时间提供建议,我将非常感激。
  • 没关系,我想为每个孩子定义实例数组是我的错,但只想有一种方法来访问它们。这是一个架构缺陷。可能,您有一个建议/页面链接,显示我应该如何实施它们?
  • 您只需在调用中将 this 设置为正确的对象,因此如果您将其作为实例的方法调用,您将是正确的 instance 数组(根据 user2736012 的评论)。
  • 谢谢..我想我仍然需要更详细地了解“this”在 javascript 中的工作原理。
  • 网上大部分的解释都不是很好,多看几遍就知道要么是错的,要么是不清楚的。最好的资源是ECMA-262,但它有点夸张。 This article on MDN 还不错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多