You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 4-binary/01-arraybuffer-binary-arrays/article.md
+48-47Lines changed: 48 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# ArrayBuffer, آرایه دودویی
1
+
# ArrayBuffer, آرایههای دودویی
2
2
3
3
در توسعهی وب، ما معمولا هنگام سروکار داشتن با فایلها(ساختن، بارگذاری کردن، دانلود کردن) به دادههای دودویی برخورد میکنیم. یکی دیگر از استفادههای رایج آن پردازش تصویر میباشد.
4
4
@@ -68,17 +68,17 @@ for(let num of view) {
68
68
69
69
## TypedArray
70
70
71
-
The common term for all these views (`Uint8Array`, `Uint32Array`, etc) is [TypedArray](https://tc39.github.io/ecma262/#sec-typedarray-objects). They share the same set of methods and properties.
71
+
اصطلاح رایج برای تمامی این viewها (`Uint8Array` و `Unit32Array` و غیره) [TypedArray](https://tc39.github.io/ecma262/#sec-typedarray-objects) میباشد. آنها متدها و ویژگیهای یکسانی دارند.
72
72
73
-
Please note, there's no constructor called `TypedArray`, it's just a common "umbrella" term to represent one of views over `ArrayBuffer`: `Int8Array`, `Uint8Array`and so on, the full list will soon follow.
73
+
لطفا به خاطر داشته باشید که هیچ سازندهای با عنوان `TypedArray` وجود ندارد و این فقط یک اصطلاح رایج برای پوشش یکی از viewهای موضوع گستردهی `ArrayBuffer` میباشد: `Int8Array` و `Uint8Array`و به همین ترتیب، لیست کامل به زودی ارائه میشود.
74
74
75
-
When you see something like `new TypedArray`, it means any of `new Int8Array`,`new Uint8Array`, etc.
75
+
هرگاه چیزی مانند `new TypedArray` مشاهده کردید، این عبارت به معنای هرکدام از `new Int8Array`،`new Uint8Array` و غیره میباشد.
76
76
77
-
Typed arrays behave like regular arrays: have indexes and are iterable.
77
+
آرایههای Typed مانند آرایههای عادی رفتار میکنند: دارای اندیش هستند و قابل پیمایش میباشند.
78
78
79
-
A typed array constructor (be it `Int8Array`or`Float64Array`, doesn't matter) behaves differently depending on argument types.
79
+
یک سازندهی آرایهی Typed(میتواند `Int8Array`یا`Float64Array` باشد، اهمیتی ندارد) با توجه به نوع آرگومان آن متفاوت رفتار میکند.
80
80
81
-
There are 5 variants of arguments:
81
+
5 نوع مختلف آرگومانها وجود دارند:
82
82
83
83
```js
84
84
newTypedArray(buffer, [byteOffset], [length]);
@@ -88,92 +88,93 @@ new TypedArray(length);
88
88
newTypedArray();
89
89
```
90
90
91
-
1. If an `ArrayBuffer` argument is supplied, the view is created over it. We used that syntax already.
92
91
93
-
Optionally we can provide `byteOffset` to start from (0 by default) and the `length` (till the end of the buffer by default), then the view will cover only a part of the `buffer`.
92
+
1. اگر یک آرگومان `ArrayBuffer` وجود داشته باشد، view بر حسب آن ساخته میشود. ما پیش از این از همین سینتکس استفاده کردیم.
93
+
94
+
ما میتوانیم به صورت اختیاری `byteOffset` تهیه کنیم که از آن شروع کنیم(به شکل پیشفرض از 0 شروع میکنیم) و یک `length` که تا آنجا ادامه دهیم(به صورت پیشفرض تا انتهای بافر ادامه میدهیم)؛ در نتیجه، view فقط بخشی از بافر را پوشش میدهد.
94
95
95
-
2.If an `Array`, or any array-like object is given, it creates a typed array of the same length and copies the content.
96
-
97
-
We can use it to pre-fill the array with the data:
96
+
2.اگر یک آرایه یا هر شی مانند آن داشته باشیم، آن شی یک آرایهی typed به همان طول میسازد و محتوا را نیز کپی میکند.
97
+
98
+
ما میتوانیم از آن برای مقداردهی اولیهی آرایه با داده استفاده کنیم:
98
99
```js run
99
100
*!*
100
101
let arr =newUint8Array([0, 1, 2, 3]);
101
102
*/!*
102
-
alert( arr.length ); // 4, created binary array of the same length
103
-
alert( arr[1] ); // 1, filled with 4 bytes (unsigned 8-bit integers) with given values
103
+
alert( arr.length ); // 4, یک آرایه دودویی به همان طول میسازد
104
+
alert( arr[1] ); // 1, با 4 بایت(اعداد صحیح بدونعلامت 8-بیتی) با مقادیر داده شده پر میشود
104
105
```
105
-
3.If another`TypedArray`is supplied, it does the same: creates a typed array of the same length and copies values. Values are converted to the newtypein the process, if needed.
106
+
3.اگر یک`TypedArray`دیگر وجود داشته باشد، به همان شکل رفتار میکند: ک آرایهی typed به همان طول میسازد و محتوا را نیز کپی میکند. در طول این فرآیند، مقادیر در صورت نیاز به نوع جدیدی تبدیل میشوند.
106
107
```js run
107
108
let arr16 = new Uint16Array([1, 1000]);
108
109
*!*
109
110
let arr8 = new Uint8Array(arr16);
110
111
*/!*
111
112
alert( arr8[0] ); // 1
112
-
alert( arr8[1] ); // 232, tried to copy 1000, but can't fit 1000 into 8 bits (explanations below)
113
+
alert( arr8[1] ); // 232, تلاش میکند 1000 را کپی کند اما نمیتواند 1000 را در 8 بیت جا دهد(توضیخات در پایین)
113
114
```
114
115
115
-
4.For a numeric argument `length`--creates the typed array to contain that many elements. Its byte length will be `length`multiplied by the number of bytes in a single item `TypedArray.BYTES_PER_ELEMENT`:
116
+
4.برای آرگومان عددی `length`--یک آرایه typed که به همان تعداد عضو دارد میسازد. طول بایت آن برابر `length`ضرب در تعداد بایتهای یک آیتم واحد `TypedArray.BYTES_PER_ELEMENT` خواهد بود:
116
117
```js run
117
-
let arr = new Uint16Array(4); // create typed array for 4 integers
118
-
alert( Uint16Array.BYTES_PER_ELEMENT ); // 2 bytes per integer
119
-
alert( arr.byteLength ); // 8 (size in bytes)
118
+
let arr = new Uint16Array(4); // برای 4 عدد صحیح میسازد typed یک آرایهی
119
+
alert( Uint16Array.BYTES_PER_ELEMENT ); // به ازای هر عدد صحیح 2 بایت
120
+
alert( arr.byteLength ); // 8 (اندازه در بایتها)
120
121
```
121
122
122
-
5.Without arguments, creates an zero-length typed array.
123
+
5.بدون آرگومانها یک آرایهی typed با طول صفر میسازد.
123
124
124
-
We can create a`TypedArray`directly, without mentioning `ArrayBuffer`. But a view cannot exist without an underlying `ArrayBuffer`, so gets created automatically in all these cases except the first one (when provided).
125
+
ما میتوانیم مستقیما یک`TypedArray`بسازیم، بدون اینکه به `ArrayBuffer` اشارهای کنیم. ولی یک view بدون `ArrayBuffer` دربرگیرنده نمیتواند وجود دشاته باشد؛ در نتیجه در همهی این موارد بجز مورد اول(هنگامی که فراهم شده است) به طور خودکار ساخته می@شود.
125
126
126
-
To access the underlying `ArrayBuffer`, there are following properties in`TypedArray`:
127
-
-`buffer`--references the`ArrayBuffer`.
128
-
-`byteLength`--the length of the`ArrayBuffer`.
127
+
برای دسترسی به `ArrayBuffer` دربرگیرنده، ویژگیهای زیر در `TypedArray` وجود دارد:
128
+
-`buffer`--ارجاع به`ArrayBuffer`.
129
+
-`byteLength`--طول`ArrayBuffer`.
129
130
130
-
So, we can always move from one view to another:
131
+
بنابراین، ما همیشه میتوانیم از یک view به دیگری برویم:
131
132
```js
132
133
let arr8 = new Uint8Array([0, 1, 2, 3]);
133
134
134
-
// another view on the same data
135
+
// دیگر در همان داده view یک
135
136
let arr16 = new Uint16Array(arr8.buffer);
136
137
```
137
138
138
139
139
-
Here's the list of typed arrays:
140
+
در ادامه لیست آرایههای typed آمده است:
141
+
142
+
-`Uint8Array`, `Uint16Array`, `Uint32Array`-- برای اعداد صحیح 8 و 16 و 32 بیتی
143
+
-`Uint8ClampedArray`-- برای اعداد صحیح 8 بیتی، آنها را "clamps" میکند(در ادامه خواهید دید)
144
+
-`Int8Array`, `Int16Array`, `Int32Array`-- برای اعداد صحیح علامتدار(میتوانند منفی باشند)
145
+
-`Float32Array`, `Float64Array`-- برای اعداد اعشاری علامتدار 32 و 64 بیتی
140
146
141
-
- `Uint8Array`, `Uint16Array`, `Uint32Array` -- for integer numbers of 8, 16 and 32 bits.
142
-
- `Uint8ClampedArray` -- for 8-bit integers, "clamps" them on assignment (see below).
143
-
- `Int8Array`, `Int16Array`, `Int32Array` -- for signed integer numbers (can be negative).
144
-
- `Float32Array`, `Float64Array` -- for signed floating-point numbers of 32 and 64 bits.
147
+
```هدر هشدار="بدون `int8` یا انواع مشابه با مقدار واحد"
145
148
146
-
```warn header="No `int8` or similar single-valued types"
147
-
Please note, despite of the names like `Int8Array`, there's no single-value type like `int`, or `int8`in JavaScript.
149
+
لطفا به خاطر داشته باشید، علیرغم نامهایی مانند `Int8Array`، هیچ نوعی با مقدار واحد مانند `int` یا `int8` در جاوااسکریپت وجود ندارد.
148
150
149
-
That's logical, as `Int8Array` is not an array of these individual values, but rather a view on `ArrayBuffer`.
151
+
این موضوع منطقی است، زیرا `Int8Array`یک آرایه از این مقادیر مجزا نیست، بلکه یک view روی`ArrayBuffer` است.
150
152
```
151
153
152
-
### Out-of-bounds behavior
154
+
### رفتار خارج از محدوده
153
155
154
-
What if we attempt to write an out-of-bounds value into a typed array? There will be no error. But extra bits are cut-off.
156
+
بنویسیم چه؟ هیچ خطایی وجود نخواهد داشت، اما بیتههای اضافی حذف خواهند شد typed اگر بخواهیم یک مقدار خارج از محدوده را در یک آرایهی
155
157
156
-
For instance, let's try to put 256 into `Uint8Array`. In binary form,256is`100000000`(9bits), but `Uint8Array`only provides 8 bits per value, that makes the available range from 0 to 255.
158
+
به ازای هر مقدار 8 بیت دارد، پس بازهی آن بین 0 تا 255 خواهد بود `Uint8Array` قرار دهیم. در حالت دودویی،256برابر`100000000`(9بیت) خواهد بود، ولی `Uint8Array`به عنوان مثال، بیایید سعی کنیم 256 را در
157
159
158
-
For bigger numbers, only the rightmost (less significant) 8 bits are stored, and the rest is cut off:
160
+
:برای اعداد بزرگتر، فقط 8 بیت سمت راست(بیتهای کمارزشتر) ذخیره میشود، و بقیه بیتها حذف میشوند
159
161
160
162

161
163
162
-
So we'll get zero.
164
+
.در نتیجه صفر دریافت میکنیم
163
165
164
-
For 257, the binary form is `100000001`(9 bits), the rightmost 8 get stored, so we'll have `1`in the array:
166
+
:برای257، حالت دودویی `100000001`(9بیت) خواهد بود،8بیت سمت راست ذخیره میشوند، در نتیجه در آرایه `1`را داریم
165
167
166
168

169
+
ذخیره میشود 2<sup>8</sup> به عبارت دیگر، باقیمانده عدد
167
170
168
-
In other words, the number modulo 2<sup>8</sup> is saved.
`Uint8ClampedArray` is special in this aspect, its behavior is different. It saves 255 for any number that is greater than 255, and 0 for any negative number. That behavior is useful for image processing.
186
+
از این نظر، `Uint8ClampedArray`خاص است و رفتار متفاوتی دارد. این مورد به ازای هر عدد بزرگتر از 255، 255 و به ازای هر عدد منفی، 0 را ذخیره میکند. این رفتار ببرای پردازش تصویر مفید است.
186
187
187
-
## TypedArray methods
188
+
## متدهای TypedArray
188
189
189
190
`TypedArray` has regular `Array` methods, with notable exceptions.
0 commit comments