起因是看了 阮一峰的一篇文章提到了這樣的說法,我當(dāng)時(shí)覺得這種說法是有問題的,因?yàn)?req 是一個(gè)對象,js按照順序執(zhí)行,如果在請求發(fā)出前指定對應(yīng)的事件回調(diào)函數(shù),那么這個(gè)請求的 onload/onerror 應(yīng)該都是默認(rèn)為空的才對。于是我用這樣的代碼測試了一下:// 1var req = new XMLHttpRequest();req.open('GET', 'https://segmentfault.com/'); req.send();req.onload = function (res){console.log(res)}; req.onerror = function (){}; // 2var req = new XMLHttpRequest();req.open('GET', 'https://segmentfault.com/'); req.onload = function (res){console.log(res)}; req.onerror = function (){}; req.send();發(fā)現(xiàn)兩者都有返回值,也就是說兩者都有回調(diào)函數(shù)。底下的回復(fù)中也有人提出了和我類似的看法,他有提及了 緩存的因素。如下:想請教一下大家的看法。謝謝
XHR 對象的 send 方法何時(shí)調(diào)用對于結(jié)果都一樣嗎?
紫衣仙女
2018-12-12 18:19:21