“朋友班”在 JavaScript 中
我有一个创建 Widget 对象的 Factory 类。 Factory 对象稍后需要回调 Widget 对象的“私有方法”以向其传递一些 ajax 信息。到目前为止,我想到的唯一实现是在 Widget 中创建一个公共方法,将私有方法返回给工厂,然后删除自身,然后工厂返回新的 Widget,同时保留指向私有方法的指针。这是一个简化的示例:
function Factory()
{
var widgetCallback = null;
this.ajaxOperation = function()
{
//some ajax calls
widgetCallback('ajaxresults');
}
this.getNewWidget = function()
{
var wid = new Widget();
widgetCallback = wid.getCallback();
return wid;
}
function Widget()
{
var state = 'no state';
var self = this;
var modifyState = function(newState)
{
state = newState;
}
this.getState = function()
{
return state;
}
this.getCallback = function()
{
delete self.getCallback;
return modifyState;
}
}
}
是否有更好的方法来实现我想要的效果,或者这是一个相当合理的方法?我知道它有效,只是好奇我是否陷入了任何我应该注意的陷阱。
I have a Factory class that creates a Widget object. The Factory object needs to callback a "private method" of the Widget object at a later time to pass it some ajax info. So far, the only implementation I've come up with is to create a public method in the Widget that returns the private method to the factory, and then deletes itself, the Factory then returns the new Widget while retaining a pointer to the private method. Here is a simplified example:
function Factory()
{
var widgetCallback = null;
this.ajaxOperation = function()
{
//some ajax calls
widgetCallback('ajaxresults');
}
this.getNewWidget = function()
{
var wid = new Widget();
widgetCallback = wid.getCallback();
return wid;
}
function Widget()
{
var state = 'no state';
var self = this;
var modifyState = function(newState)
{
state = newState;
}
this.getState = function()
{
return state;
}
this.getCallback = function()
{
delete self.getCallback;
return modifyState;
}
}
}
Is there a better way to achieve the effect I'm after or is this a fairly reasonable approach? I know it works, just curious if I'm stepping into any pitfalls I should be aware of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需让您的构造函数返回一个
Tuple
或者只需使用闭包作用域直接在您的
Widget
构造函数中编辑widgetCallback
Just get your constructor to return a
Tuple<Widget, function>
Alternative just use closure scope to edit
widgetCallback
directly in yourWidget
constructor我对面向对象的 JavaScript 不够熟悉(我在 GWT 代码中主要使用一两个衬垫)来实际给出真正的答案(但我发现我的回复对于评论来说有点长......)
我认为自我修改类,听起来像是一个潜在的陷阱。
我个人更喜欢JavaScript、Ruby等语言,这些语言对你能做的事情没有限制(即使我在工作中必须使用Java+GWT,呵呵),但你要靠自律不做傻事。我宁愿在方法名称前加上“_”前缀(并且只是避免在不应该使用的地方使用它),而不是尝试强制执行私有方法。由于 JavaScript 本质上对你可以做的疯狂事情是非常不受限制的,所以无论如何它都需要大量的纪律。
如果您在使用后删除了某个方法;为了某种程度的保护它,你能不能简单地添加一个新的方法来做同样的事情?无论如何,你仍然会依赖你(和其他人)的自律和理智,不是吗?
I'm not familiar enough with object oriented JavaScript (I use mostly one-or-two liners inside GWT code) to actually give an Real Answer (But I found that my response were a bit long for a comment...)
I think self-modifying classes, sounds like a major potential for gotcha's.
I personally prefer languages such as JavaScript, Ruby, etc. that are not restrictive in what you can do (even if I have to use Java+GWT at work, hehe), but where you rely self discipline to not do stupid things. I would rather prefix the method name with "_" (and simply avoid using it where I should not), than try to enforce private methods. Since JavaScript by nature is very unrestricted in what crazy things you may do, it requires a lot of discipline anyway.
If you deleted a method after use; to kind-of-protecting it, could you not just as easily add a new method to do the same? You would still rely on your (and others) self discipline and sanity anyway, aren't you?