Javascript:如何覆盖某些类中的公共方法?

发布于 2024-08-12 04:47:53 字数 801 浏览 3 评论 0 原文

我有这样的 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 的函数。

任何想法有什么问题吗?

I have such JS class that have to be tested:

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");  
        }   
     }  
}

I want to override method initRequest for testing purposes. I tried to do something like that

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

Still calling of the initRequest method calls actually the original code, but not the function that I tried to pass to instance.initRequest.

Any ideas what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏の忆 2024-08-19 04:47:53

查看 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

月牙弯弯 2024-08-19 04:47:53

在第一个示例中,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?

自控 2024-08-19 04:47:53

由于你的英语,我在理解你的问题时遇到了一些困难,但无论如何我都会尝试回答(如果我回答错误的问题,请告诉我)。

我认为您想要做的是:

SomeClass = function {  
    // some stuff that uses initRequest    
    this.initRequest = function() {  
        return request;
     }  
}

换句话说,您想用新函数覆盖原始 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:

SomeClass = function {  
    // some stuff that uses initRequest    
    this.initRequest = function() {  
        return request;
     }  
}

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?

临走之时 2024-08-19 04:47:53
SomeClass.prototype.initRequest = function() 
  {  
  return request;
  }  
SomeClass.prototype.initRequest = function() 
  {  
  return request;
  }  
断舍离 2024-08-19 04:47:53

首先,此代码示例不是 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 of SomeClass 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文