[英]JavaScript: How do I access the prototype of an object inside another object?
So I think I'm going crazy here. 所以我想我在这里疯了。 I'm trying to use prototype in my code, so I will have a Layer object that will have a Figure Object that will have a _newFigure function.
我正在尝试在代码中使用原型,因此我将具有一个Layer对象,该对象将具有一个具有_newFigure函数的Figure对象。
Layer = function(){}
Layer.prototype.figure = function(){}
Layer.prototype.figure.prototype._newFigure = function(){
//(Creates new Figure)
}
What I'm expecting in pseudo-code: 我对伪代码的期望:
[LayerObject]{
figure:{
_newFigure:function(){
//(Creates new Figure)
}
}
}
Now that, to me , would be the logical thing to do, but for some reason it doens't work. 现在, 对我来说 ,这将是合乎逻辑的事情,但是由于某种原因,它不起作用。 Do you now how can I achieve that?
您现在如何实现? Keep in mind I'm using prototype for performance reasons.
请记住,出于性能原因,我正在使用原型。
-- thanks in advance -- - 预先感谢 -
UPDATE--------------------------------------------------------------------------------------- UPDATE ------------------------------------------------- --------------------------------------
As Nick said "Why does Layer need to have a Figure on it's prototype? It seems like Layer and Figure our separate entities, and a Layer can have a Figure instance attached to it. Just a thought : )" 正如Nick所说: “为什么Layer的原型上需要有一个Figure?看来Layer和Figure是我们独立的实体,并且Layer可以附加一个Figure实例。只是一个想法:)”
So, I've updated the code (now working) 所以,我更新了代码(现在可以正常使用)
Layer = function(){
this.figure = new figureHandler(this);
}
function figureHandler(layer){
this.layer = layer;
}
figureHandler.prototype._newFigure = function(){
//(Creates new Figure)
}
-- See ya! - 再见! Thanks for your help :) --
感谢您的帮助:) -
Would something like the following work for you? 像下面这样的东西对您有用吗?
Figure = function() {};
Layer = function() {};
Layer.prototype.figure = function() {
return new Figure();
};
It's a bit confusing to me why you would want an interface like layer.figure._newFigure
when layer.figure
could create a new figure object for you. 我有点困惑,为什么当
layer.figure
可以为您创建一个新的图形对象时,您想要一个诸如layer.figure._newFigure
类的接口。 Perhaps your use case just isn't clear enough to me. 也许您的用例对我来说还不够清楚。
EDIT: Based on the comment to my original response, I assume you mean something like this: 编辑:基于对我的原始回复的评论,我认为您的意思是这样的:
Figure = function(layer) {
this.layer = layer;
};
Layer = function() {};
Layer.prototype.figure = function() {
return new Figure(this);
};
I think you may be looking for something like this: 我认为您可能正在寻找这样的东西:
Figure has a Layer instance and a Layer instance has multiple figures. 图具有一个Layer实例,而Layer实例具有多个图。 Layer may have a factory that will create Figures having the current Layer instance:
Layer可能有一个工厂,该工厂将创建具有当前Layer实例的Figures:
Layer = function Layer(){
this.figures = [];
};
Layer.prototype.addFigure = function addFigure(arg){
arg = arg || {};
arg.layer = this;
this.figures.push(new Figure(arg));
return this;//now you can stack functions
}
Figure = function Figure(arg){
arg = arg || {};
//layer is not optional
this.layer = arg.layer
|| (function(){
throw new Error('Cannot create figure without layer');
}());
this.shape = arg.shape || 'square';
}
var l = new Layer();
l.addFigure({shape:'round'})//stacking next addFigure on next line
.addFigure();//defaults to square
console.log(l.figures);
More info on constructor functions and prototype here: https://stackoverflow.com/a/16063711/1641941 有关构造函数和原型的更多信息,请参见: https : //stackoverflow.com/a/16063711/1641941
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.