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

      <small id='9mWKd'></small><noframes id='9mWKd'>

      1. <legend id='9mWKd'><style id='9mWKd'><dir id='9mWKd'><q id='9mWKd'></q></dir></style></legend>
          <bdo id='9mWKd'></bdo><ul id='9mWKd'></ul>

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

        How can I create a XMLHttpRequest wrapper/proxy?(如何创建 XMLHttpRequest 包装器/代理?)

            <tbody id='qyu7a'></tbody>

          <tfoot id='qyu7a'></tfoot>

            <legend id='qyu7a'><style id='qyu7a'><dir id='qyu7a'><q id='qyu7a'></q></dir></style></legend>

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

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

                  本文介绍了如何创建 XMLHttpRequest 包装器/代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  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 包装器/代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                  XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                  How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
                  quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
                        <tbody id='RweYQ'></tbody>

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

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