如何延长XMLHtt prequest对象的JavaScript?对象、XMLHtt、prequest、JavaScript

由网友(零下2℃的心)分享简介:我想扩展现有XMLHtt prequest对象,以便它应该与所有的浏览器。现在,我一直在低谷与 JS继承并不过的事情开始我想看到的好例子之前它。 HTML5具有上传和进度事件的东西,我想,如果在该功能不支持不引入JS错误的客户端,可以表现甚至继承了新的对象来实现。所以我想实现这样的事情。类XMLHtt prequest...

我想扩展现有XMLHtt prequest对象,以便它应该与所有的浏览器。现在,我一直在低谷与 JS继承并不过的事情开始我想看到的好例子之前它。

HTML5具有上传和进度事件的东西,我想,如果在该功能不支持不引入JS错误的客户端,可以表现甚至继承了新的对象来实现。所以我想实现这样的事情。

 类XMLHtt prequest {}
类UploadXMLHtt prequest:XMLHtt prequest {}
 

其中附加的方法可以附着到UploadXMLHtt prequest类像以下

  UploadXMLHtt prequest.prototype.uploadFile =功能(文件){

}
 

鉴于YUI,jQuery和其他人都在市场上良好的没有人真的想这样做,这使得它有点困难,我找到很好的资源。

解决方案

不要这样做。 XMLHtt prequest 是主机对象,你不应该试图把它扩大。引用Kangax:

  

接下来的问题DOM扩展   该 DOM对象是宿主对象,然后   主机对象是最坏的一群。通过   规范(ECMA-262第3编),主机   对象可以做的事情,没有   其他对象甚至可以梦想。至   报价相关部门 [8.6.2] :

          

主机对象可能实现这些     内部方法与任何     实施相关的行为,或     它可能是一个宿主对象     仅实现一些内部方法     而不是其他人。的

   JavaScript 标准之 Map对象

这也意味着主机对象可以通过使用原型不允许扩展。

然而,如Kangax还advices,你可以创建一个围绕 XMLHtt prequest 包装,做任何你想用它。

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.

解决方案

Don'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.

阅读全文

相关推荐

最新文章