• <small id='FhGv0'></small><noframes id='FhGv0'>

    1. <legend id='FhGv0'><style id='FhGv0'><dir id='FhGv0'><q id='FhGv0'></q></dir></style></legend>
    2. <i id='FhGv0'><tr id='FhGv0'><dt id='FhGv0'><q id='FhGv0'><span id='FhGv0'><b id='FhGv0'><form id='FhGv0'><ins id='FhGv0'></ins><ul id='FhGv0'></ul><sub id='FhGv0'></sub></form><legend id='FhGv0'></legend><bdo id='FhGv0'><pre id='FhGv0'><center id='FhGv0'></center></pre></bdo></b><th id='FhGv0'></th></span></q></dt></tr></i><div id='FhGv0'><tfoot id='FhGv0'></tfoot><dl id='FhGv0'><fieldset id='FhGv0'></fieldset></dl></div>
        <bdo id='FhGv0'></bdo><ul id='FhGv0'></ul>

        <tfoot id='FhGv0'></tfoot>

        如何创建 XMLHttpRequest 包装器/代理?

        时间:2023-10-14

          <small id='ODy0N'></small><noframes id='ODy0N'>

            <tbody id='ODy0N'></tbody>

            1. <i id='ODy0N'><tr id='ODy0N'><dt id='ODy0N'><q id='ODy0N'><span id='ODy0N'><b id='ODy0N'><form id='ODy0N'><ins id='ODy0N'></ins><ul id='ODy0N'></ul><sub id='ODy0N'></sub></form><legend id='ODy0N'></legend><bdo id='ODy0N'><pre id='ODy0N'><center id='ODy0N'></center></pre></bdo></b><th id='ODy0N'></th></span></q></dt></tr></i><div id='ODy0N'><tfoot id='ODy0N'></tfoot><dl id='ODy0N'><fieldset id='ODy0N'></fieldset></dl></div>
              <legend id='ODy0N'><style id='ODy0N'><dir id='ODy0N'><q id='ODy0N'></q></dir></style></legend>
                <bdo id='ODy0N'></bdo><ul id='ODy0N'></ul>
              • <tfoot id='ODy0N'></tfoot>
                  本文介绍了如何创建 XMLHttpRequest 包装器/代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  想到的这些方法,各有什么优缺点?

                  These methods that come to mind, what are the pros and cons of each?

                  方法一:增强原生实例

                  var _XMLHttpRequest = XMLHttpRequest;
                  XMLHttpRequest = function() {
                      var xhr = new _XMLHttpRequest();
                  
                      // augment/wrap/modify here
                      var _open = xhr.open;
                      xhr.open = function() {
                          // custom stuff
                          return _open.apply(this, arguments);
                      }
                  
                      return xhr;
                  }
                  

                  方法2:子类"原生XMLHttpRequest

                  Method 2: Sub-"class" native XMLHttpRequest

                  var _XMLHttpRequest = XMLHttpRequest;
                  XMLHttpRequest = function() {
                      // definePropertys here etc
                  }
                  
                  XMLHttpRequest.prototype = new _XMLHttpRequest());
                  // OR
                  XMLHttpRequest.prototype = Object.create(_XMLHttpRequest);
                  
                  // custom wrapped methods on prototype here
                  XMLHttpRequest.prototype.open = function() {
                      // custom stuff
                      return _XMLHttpRequest.prototype.open.apply(this, arguments);
                  }
                  

                  方法三:完全代理原生 XMLHttpRequest

                  Method 3: Full proxy to native XMLHttpRequest

                  var _XMLHttpRequest = XMLHttpRequest;
                  XMLHttpRequest = function() {
                      this.xhr = new _XMLHttpRequest();
                  }
                  
                  // proxy ALL methods/properties
                  XMLHttpRequest.prototype.open = function() {
                      // custom stuff
                      return this.xhr.open.apply(this.xhr, arguments);
                  }
                  

                  推荐答案

                  根据 JS 引擎,方法 1 会产生相当大的开销,因为每当实例化 XHR 时都会重新定义 xhr.open.

                  Depending on the JS engine, method 1 produces considerable overhead, since xhr.open is redefined whenever XHR is instantiated.

                  方法 2 让我想为什么首先需要 new _XMLHttpRequest"?有轻微副作用的感觉,但似乎效果很好.

                  Method 2 makes me think "why would you need the new _XMLHttpRequest in the first place"? There's a minor feeling of undesired side effects, but it appears to work just fine.

                  方法 3:简单、老派,但不会立即奏效.(考虑读取属性)

                  Method 3: simple, old-school, but it won't work straight-away. (Think about reading properties)

                  一般来说,我个人不太愿意覆盖浏览器对象,所以这对所有三种方法来说都是一个很大的缺点.最好使用其他变量,例如 ProxyXHR(只是我的 2 美分)

                  In general, I'm personally reluctant when it comes to overwriting browser objects, so that would be a big con to all three methods. Better use some other variable like ProxyXHR (just my 2 cents)

                  这篇关于如何创建 XMLHttpRequest 包装器/代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Service Worker 可以响应同步的 XHR 请求吗? 下一篇:Ajax 密集型页面:重用同一个 XMLHttpRequest 对象还是

                  相关文章

                  最新文章

                  <i id='AF7V5'><tr id='AF7V5'><dt id='AF7V5'><q id='AF7V5'><span id='AF7V5'><b id='AF7V5'><form id='AF7V5'><ins id='AF7V5'></ins><ul id='AF7V5'></ul><sub id='AF7V5'></sub></form><legend id='AF7V5'></legend><bdo id='AF7V5'><pre id='AF7V5'><center id='AF7V5'></center></pre></bdo></b><th id='AF7V5'></th></span></q></dt></tr></i><div id='AF7V5'><tfoot id='AF7V5'></tfoot><dl id='AF7V5'><fieldset id='AF7V5'></fieldset></dl></div>
                    <bdo id='AF7V5'></bdo><ul id='AF7V5'></ul>

                  1. <small id='AF7V5'></small><noframes id='AF7V5'>

                  2. <legend id='AF7V5'><style id='AF7V5'><dir id='AF7V5'><q id='AF7V5'></q></dir></style></legend>
                  3. <tfoot id='AF7V5'></tfoot>