I would like to extend the existing XMLHttpRequest
object so that it should work with all the browsers. Now I have been trough with JS inheritance and things however before starting I would like to see good example of it.
HTML5 has upload and progress events stuff which I would like to implement in inherited new object which can behave even if the feature is not supported by not introducing JS errors to client side. So I would like to achieve something like this:
Class XMLHttpRequest{}
Class UploadXMLHttpRequest: XMLHttpRequest{}
Where additional methods can be attached to UploadXMLHttpRequest
class like following.
UploadXMLHttpRequest.prototype.uploadFile = function(file){
}
Considering YUI, jQuery and others are good in market no one really wants to do this made it little difficult for me to find good resources.
I would like to extend the existing XMLHttpRequest
object so that it should work with all the browsers. Now I have been trough with JS inheritance and things however before starting I would like to see good example of it.
HTML5 has upload and progress events stuff which I would like to implement in inherited new object which can behave even if the feature is not supported by not introducing JS errors to client side. So I would like to achieve something like this:
Class XMLHttpRequest{}
Class UploadXMLHttpRequest: XMLHttpRequest{}
Where additional methods can be attached to UploadXMLHttpRequest
class like following.
UploadXMLHttpRequest.prototype.uploadFile = function(file){
}
Considering YUI, jQuery and others are good in market no one really wants to do this made it little difficult for me to find good resources.
Share Improve this question edited May 21, 2022 at 22:43 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 8, 2011 at 10:36 Anil NamdeAnil Namde 6,61811 gold badges66 silver badges101 bronze badges 5- unatainable goal since not all browsers have a XMLHttpRequest class. and by not all browsers, I mean Internet explorer. What you have to do is to use those 'good in market' libraries and use their wrappers to make your ajax calls. – BiAiB Commented Feb 8, 2011 at 10:44
- 1 possible duplicate of Failed to override the getResponseHeader method on XMLHttpRequest – Marcel Korpel Commented Feb 8, 2011 at 10:44
- No i know ActiveXObject in IE and XMLHttpRequest in others. What i would like to know is what is good way i can extend the XMLHttpRequest so that i can have a clean and safe API for regular use. – Anil Namde Commented Feb 8, 2011 at 10:50
- Look at code of "good in market" libraries =) – kirilloid Commented Feb 8, 2011 at 10:55
- You can use the frameworks previously named, they have a clean safe, widely used and tested API, so unless you have good reasons, in which case it would be could to expose them here, why wouldn't you use them ? – BiAiB Commented Feb 8, 2011 at 10:58
1 Answer
Reset to default 8Don't do this. XMLHttpRequest
is a host object and you should not try to extend it. To quote Kangax:
Next problem with DOM extension is that DOM objects are host objects, and host objects are the worst bunch. By specification (ECMA-262 3rd. ed), host objects are allowed to do things, no other objects can even dream of. To quote relevant section [8.6.2]:
Host objects may implement these internal methods with any implementation-dependent behaviour, or it may be that a host object implements only some internal methods and not others.
This also means that host objects may disallow extension by using prototype
.
However, as Kangax also advices, you can create a wrapper around XMLHttpRequest
and do whatever you like with it.