Response
接口的 clone() 方法创建了一个响应对象的克隆,这个对象在所有方面都是相同的,但是存储在一个不同的变量中。
如果已经使用了响应 Body
,clone()
会抛出TypeError
。 实际上,clone()存在的主要原因是允许多次使用Body
对象(当它们是一次性使用的时候)。
语法
var response2 = response1.clone();
Parameters
None.
Value
一个 Response
对象.
示例
在我们的 Fetch Response 克隆示例 (请参阅 Fetch Response clone live) 我们使用Request()
构造函数创建一个新的 Request
来传递一个 JPG 路径。 然后我们使用 fetch()
获取这个请求。 当 fetch resolve 时,我们克隆它,使用两个Body.blob
调用从两个响应中提取blob,使用URL.createObjectURL
从blob创建对象URL,并将它们显示在两个单独的<img>
元素中。
var image1 = document.querySelector('.img1');
var image2 = document.querySelector('.img2');
var myRequest = new Request('flowers.jpg');
fetch(myRequest).then(function(response) {
var response2 = response.clone();
response.blob().then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
image1.src = objectURL;
});
response2.blob().then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
image2.src = objectURL;
});
});
Specifications
Specification | Status | Comment |
---|---|---|
Fetch clone() |
Living Standard | Initial definition |
Browser compatibility
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 42 41[1] |
(Yes) | 39 (39) 34[1] |
未实现 |
29 |
未实现 |
Feature | Android | Edge | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | 未实现 | (Yes) | 未实现 | 未实现 | 未实现 | 未实现 | 未实现 | 未实现 |
[1] 此功能是在首选项后面实现的。