English 中文(简体)
Javascript: how to override public method in some class?
原标题:

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?

问题回答

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

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?

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?

SomeClass.prototype.initRequest = function() 
  {  
  return request;
  }  

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签