1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
---
title: ArrayBuffer.transfer()
slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transfer
translation_of: Archive/Web/JavaScript/ArrayBuffer.transfer
---
<div>{{JSRef}} {{SeeCompatTable}}</div>
<p> 静态<code><strong>ArrayBuf</strong></code><strong>fer.transfer()</strong> 方法返回一个新的ArrayBuffer, 其内容取自oldBuffer的数据,并且根据 newByteLength 的大小来对数据进行截取或者以0扩展。 如果 newByteLength 未定义,则使用 oldBuffer 的byteLength。这个操作使得 oldBuffer 处于被移除的状态。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">ArrayBuffer.transfer(oldBuffer [, newByteLength]);</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>oldBuffer</code></dt>
<dd> 要转移的{{jsxref("ArrayBuffer")}}对象。</dd>
<dt>newByteLength</dt>
<dd>新 <code>ArrayBuffer</code> 对象的字节长度。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>一个新的ArrayBuffer对象。</p>
<h2 id="描述">描述</h2>
<p><code>ArrayBuffer.transfer()</code>方法允许你增长和移除 <code>ArrayBuffer</code> 对象。不需复制就能增长一个ArrayBuffer的功能,对于大的缓冲区来说,有速度优势 (类似realloc) 。当释放底层内存时,移除ArrayBuffer的功能给开发者提供了显式控制。这避免了必须丢弃所有引用和等待垃圾回收。</p>
<h2 id="示例">示例</h2>
<pre class="brush: js">var buf1 = new ArrayBuffer(40);
new Int32Array(buf1)[0] = 42;
var buf2 = ArrayBuffer.transfer(buf1, 80);
buf1.byteLength; // 0 but if you use the polyfill then the value is still 40
buf2.byteLength; // 80
new Int32Array(buf2)[0]; // 42
var buf3 = ArrayBuffer.transfer(buf2, 0);
buf2.byteLength; // 0 but if you use the polyfill then the value is still 80
buf3.byteLength; // 0
</pre>
<h2 id="Polyfill">Polyfill</h2>
<p>You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of transfer<font face="Consolas, Liberation Mono, Courier, monospace">()</font> in implementations that do not natively support it. This is not the exact equivalent of this API, but this function transfers data from one ArrayBuffer to another ArrayBuffer.</p>
<pre>if(!ArrayBuffer.transfer) {
ArrayBuffer.transfer = function (source, length) {
source = Object(source);
var dest = new ArrayBuffer(length);
if(!(source instanceof ArrayBuffer) || !(dest instanceof ArrayBuffer)) {
throw new TypeError("Source and destination must be ArrayBuffer instances");
}
if(dest.byteLength >= source.byteLength) {
var nextOffset = 0;
var leftBytes = source.byteLength;
var wordSizes = [8, 4, 2, 1];
wordSizes.forEach(function (_wordSize_) {
if (leftBytes >= _wordSize_) {
var done = transferWith(_wordSize_, source, dest, nextOffset, leftBytes);
nextOffset = done.nextOffset;
leftBytes = done.leftBytes;
}
});
}
return dest;
function transferWith(wordSize, source, dest, nextOffset, leftBytes) {
var ViewClass = Uint8Array;
switch (wordSize) {
case 8:
ViewClass = Float64Array;
break;
case 4:
ViewClass = Float32Array;
break;
case 2:
ViewClass = Uint16Array;
break;
case 1:
ViewClass = Uint8Array;
break;
default:
ViewClass = Uint8Array;
break;
}
var view_source = new ViewClass(source, nextOffset, Math.trunc(leftBytes / wordSize));
var view_dest = new ViewClass(dest, nextOffset, Math.trunc(leftBytes / wordSize));
for(var i=0; i<view_dest.length; i++) {
view_dest[i] = view_source[i];
}
return {
nextOffset : view_source.byteOffset + view_source.byteLength,
leftBytes : source.byteLength - (view_source.byteOffset + view_source.byteLength)
}
}
};
}</pre>
<h2 id="规范">规范</h2>
<p>Not part of any current specification draft document, but <a href="https://esdiscuss.org/topic/sept-23-2014-meeting-notes">has been</a> <a href="https://gist.github.com/lukewagner/2735af7eea411e18cf20">proposed</a> for a future ECMA-262 edition.</p>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.ArrayBuffer.transfer")}}</p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Typed_arrays" title="en/JavaScript typed arrays">JavaScript typed arrays</a></li>
</ul>
|