在JavaScript中,call()是同步的,而apply()是异步的吗?

时间:2019-01-04 19:43:20

标签: javascript performance asynchronous synchronous

我正在尝试拦截函数客户端函数调用,并且我编写了此脚本来覆盖XMLHttpRequest.prototype.open函数。但是,使用call()会中断Chrome中的抓取请求!给出的原因-

  

不赞成在主线程上使用同步XMLHttpRequest,因为它会对最终用户的体验产生不利影响。如需更多帮助,请检查https://xhr.spec.whatwg.org/

当我将函数call()替换为apply()时,警告消失,请求再次起作用。

原始:

!(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
    // dostuff
    open.call(this, method, newURL, async, user, password);
  };
})(XMLHttpRequest.prototype.open);

新功能(工作中):

!(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
    // dostuff
    open.apply(this, arguments);
  };
})(XMLHttpRequest.prototype.open);

尽管代码可以正常工作-我担心这不会改变潜在的问题。当获取规范跟上时,它可能会再次中断拦截功能。有谁知道在call()和apply()中是否确实存在同步与异步的区别?

0 个答案:

没有答案