Javascript:如何覆盖某些类中的公共方法?
我有这样的 JS 类必须进行测试:
SomeClass = function {
// some stuff that uses initRequest
this.initRequest = function() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
我想重写 initRequest 方法以进行测试。我尝试做类似的事情
var request = new MockXmlHttpRequest();
var instance = new SomeClass();
instance.initRequest = function() {
return request;
};
// some calls of the SomeClass methods that use initRequest
// some test code with assertions for the request
仍然调用 initRequest
方法实际上调用了原始代码,但不是我尝试传递给 instance.initRequest
的函数。
任何想法有什么问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 YUI 的扩展功能。它使这一切变得非常干净,另外,如果您确实愿意,您仍然可以访问超类的方法。您可能也会对他们的增强对象感兴趣。
这是链接 http://developer.yahoo.com/yui/examples/雅虎/yahoo_extend.html
Check out YUI's extend functionality. It makes all this really clean, PLUS, you can still access the method of the super class if you really want to. Their augment object might also be of interest to you.
Here is the link http://developer.yahoo.com/yui/examples/yahoo/yahoo_extend.html
在第一个示例中,initRequest 在每次调用后返回一个新对象;在第二个示例中,对 initRequest 的所有调用都会返回对唯一(模拟)对象的引用。
您提供的代码示例中的请求对象未更改。 instance.initRequest 简单地返回对请求的引用。为什么期望它会改变?
In your first example, initRequest returns a new object after each call; in the second example, all calls to initRequest return a reference to the one and only (mock) object.
The request object is not changed in the code example you provide. instance.initRequest simple returns a reference to request. Why do expect it to have changed?
由于你的英语,我在理解你的问题时遇到了一些困难,但无论如何我都会尝试回答(如果我回答错误的问题,请告诉我)。
我认为您想要做的是:
换句话说,您想用新函数覆盖原始 SomeClass 对象。除非您这样做,否则新的 SomeClass 对象将不会使用您的测试方法,它们只会使用原始方法。
但是,如果您只想为特定对象而不是整个类重写该方法,那么您所拥有的应该可以工作。如果是这样的话,您能澄清一下究竟是什么不起作用吗?
I'm having a little trouble understanding your question because of your English, but I'll try to answer anyway (and if I answer the wrong question just let me know).
I think what you want to do is:
In other words, you want to overwrite the original SomeClass object with a new function. Unless you do that new SomeClass objects won't use your test method, they'll just use the original method.
However, if you only want to override that method for a specific object, not for the whole class, what you have there should work. If that's the case, could you please clarify what exactly isn't working about it?
首先,此代码示例不是 JavaScript 中 OOP 的最佳方式 - 请使用
prototype
代替。另请注意,您的示例在我的浏览器中抛出错误(SomeClass
函数的声明无效,也许只是输入错误)。最后,instance.initRequest
实际上返回了您所期望的对象(我在 Google Chrome 上运行了它) - 请检查我的样本。First of all, this code sample is not the best way of OOP in JavaScript - use
prototype
instead. Another remark, your sample throws error in my browser (invalid declarartion ofSomeClass
function, maybe it's only typing mistake). And at last,instance.initRequest
actually returns (I've run it at Google Chrome) object you have expected - check please my sample.