Javascript - 原型继承问题和可能的一般理解问题
我开始学习 JavaScript,在使用了多年但并不真正了解发生了什么之后,我决定是时候对此做点什么了。此外,我还从事一个可以使用 JavaScript 来编写应用程序脚本的项目。
所以,说了这么多,我的问题与原型继承有关,我认为我现在知道它是如何工作的,但可能存在问题。基本思想是每个对象(好吧,不是每个对象 - 是的;-))都有一个原型,如果实际对象无法处理它,直到到达原型链的末尾(一个原型的原型),所有请求都会被委托给该原型。对象也可以有原型,对吧?)。
在我的应用程序中,将会发生大规模过滤,我想为用户提供一种链接简单过滤器命令的方法(这不是非常技术性的,它应该尽可能简单),从而通过每个附加调用缩小结果集。我是什么意思?史密斯。像这样:
zoo.getAnimals().getAnimalsWithFurColor("red").getMammals()
zoo
始终在范围内,并且是一个包含动物和动物园其他属性的对象。因此,我希望示例中的所有方法调用都会返回一个像这样的对象(我们将其称为 Container
):
{
data: [harryTheElephant, sallyThePenguin, timTheWoodpecker],
someFilterMethod: function() {
return new Container(data.filter(function(animal) {
return animal.someProperty == someValue;
});
}
}
到目前为止,一切都很好。但我也希望动物园成为一个容器。这就是我真正的问题发挥作用的地方:由于动物园必须做一些非常昂贵的事情来检索起始动物组,我想延迟加载它们。当动物园获得 getAnimals()
请求获取数据。
我的想法是将动物园的原型设置为一个从动物园获取数据的容器。但我找不到从原型(容器)访问动物园的 getAnimals() 方法的方法。我用 this
尝试过,但找不到该方法。
有什么想法如何编写可以完成我想做的事情的代码吗?请注意,我不在浏览器环境中,我在我的应用程序中嵌入了 JavaScript (1.7)。
谢谢!
I started to learn JavaScript, after years of using it without really knowing what's going on, i decided it is time to do something about that. Additionally, I work on a project where JavaScripts can be used to script the Application.
So, enough of all the yada-yada, my problem relates to prototypal inheritance, I think I know how it works by now, but there might be issues. The basic Idea is that every object (well, not every object - yeah ;-)) has a prototype where all requests are delegated to if the actual object cannot handle it until the end of the prototype chain has been reached (the prototype of an object can have a prototype too, right?).
In my Application, massive filtering is going to happen and I want to give the users (which are not very technical, It should be as easy as possible.) a way to chain simple filter commands narrowing the result set with each additional call. What do I mean? Smth. like this:
zoo.getAnimals().getAnimalsWithFurColor("red").getMammals()
zoo
is always in scope and is an object containing animals and additional properties for the zoo. So, I would have an object like this (lets call it Container
) to be returned by all method calls in the example:
{
data: [harryTheElephant, sallyThePenguin, timTheWoodpecker],
someFilterMethod: function() {
return new Container(data.filter(function(animal) {
return animal.someProperty == someValue;
});
}
}
So far, so good. But i want the Zoo to be a container too. This is where my real question comes into play: As the zoo has to do some very expensive stuff to retrieve the starting set of animals, i want to lazy-load them. When the zoo gets an getAnimals()
request the data is fetched.
My Idea was to set the prototype of the Zoo to be a Container which obtains its data from the zoo. But I cannot find a way to access the getAnimals()
method from the zoo from the prototype (the container). I tried it with this
but the method cannot be found.
Any Ideas how to write code that does what i want to do? Please note that i'm not in a Browser environment, i embed JavaScript (1.7) in my Application.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于一个对象来说,访问(原型地)从它继承的东西的属性没有多大意义。例如,如果
Cat
有一个meow()
方法,那么Mammal
引用meow()
会很奇怪>。同样,如果Zoo
是从Container
派生的,那么Container
引用getAnimals()
会很奇怪。我的建议是,要完成延迟加载,请将一个返回数组的访问器函数传递给
Container
构造函数,而不是直接传递数组。这可能看起来像:It doesn't make much sense for an object to access the properties of something that (prototypally) inherits from it. E.g., if a
Cat
has ameow()
method, it would be weird forMammal
to referencemeow()
. Similarly, it would be weird forContainer
to referencegetAnimals()
, ifZoo
is the one deriving fromContainer
.My suggestion, to accomplish your lazy-loading, would be to pass an accessor function that returns an array into the
Container
constructor, instead of passing an array directly. This might look something like:对象继承自其内部原型,而不是其公共原型。
要实现链接,只需让每个方法返回实例即可。
例如
,但是,链接并不是一个好主意,因为它使调试变得困难。如果您将一堆命令链接在一起并放在一个语句或表达式中并且失败了,您如何找出哪一个失败了?
编辑
您可能想做类似的事情:
Objects inherit from their internal prototype, not their public prototype.
To implement chaining, just have each method return the instance.
e.g.
However, chaining isn't a good idea because it makes debugging difficult. If you chain a bunch of commands together and in a single statement or expression and it fails, how do you find out which one failed?
Edit
You may want to do something like: