admin管理员组文章数量:1391981
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.
本文标签: ajaxHow to extend XMLHttpRequest object in JavaScriptStack Overflow
版权声明:本文标题:ajax - How to extend XMLHttpRequest object in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744693621a2620146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论