Skip to content

Commit 9adc0f6

Browse files
authored
Update article.md till TypedArray Methods
1 parent 295ca16 commit 9adc0f6

1 file changed

Lines changed: 48 additions & 47 deletions

File tree

4-binary/01-arraybuffer-binary-arrays/article.md

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ArrayBuffer, آرایه دودویی
1+
# ArrayBuffer, آرایه‌های دودویی
22

33
در توسعه‌ی وب، ما معمولا هنگام سروکار داشتن با فایل‌ها(ساختن، بارگذاری کردن، دانلود کردن) به داده‌های دودویی برخورد می‌کنیم. یکی دیگر از استفاده‌های رایج آن پردازش تصویر می‌باشد.
44

@@ -68,17 +68,17 @@ for(let num of view) {
6868

6969
## TypedArray
7070

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) می‌باشد. آن‌ها متدها و ویژگی‌های یکسانی دارند.
7272

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` و به همین ترتیب، لیست کامل به زودی ارائه می‌شود.
7474

75-
When you see something like `new TypedArray`, it means any of `new Int8Array`, `new Uint8Array`, etc.
75+
هرگاه چیزی مانند `new TypedArray` مشاهده کردید، این عبارت به معنای هرکدام از `new Int8Array`، `new Uint8Array` و غیره می‌باشد.
7676

77-
Typed arrays behave like regular arrays: have indexes and are iterable.
77+
آرایه‌های Typed مانند آرایه‌های عادی رفتار می‌کنند: دارای اندیش هستند و قابل پیمایش می‌باشند.
7878

79-
A typed array constructor (be it `Int8Array` or `Float64Array`, doesn't matter) behaves differently depending on argument types.
79+
یک سازنده‌ی آرایه‌ی Typed(می‌تواند `Int8Array` یا `Float64Array` باشد، اهمیتی ندارد) با توجه به نوع آرگومان آن متفاوت رفتار می‌کند.
8080

81-
There are 5 variants of arguments:
81+
5 نوع مختلف آرگومان‌ها وجود دارند:
8282

8383
```js
8484
new TypedArray(buffer, [byteOffset], [length]);
@@ -88,92 +88,93 @@ new TypedArray(length);
8888
new TypedArray();
8989
```
9090

91-
1. If an `ArrayBuffer` argument is supplied, the view is created over it. We used that syntax already.
9291

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 فقط بخشی از بافر را پوشش می‌دهد.
9495

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+
ما می‌توانیم از آن برای مقداردهی اولیه‌ی آرایه با داده استفاده کنیم:
9899
```js run
99100
*!*
100101
let arr = new Uint8Array([0, 1, 2, 3]);
101102
*/!*
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-بیتی) با مقادیر داده شده پر می‌شود
104105
```
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 new type in the process, if needed.
106+
3. اگر یک `TypedArray` دیگر وجود داشته باشد، به همان شکل رفتار می‌کند: ک آرایه‌ی typed به همان طول می‌سازد و محتوا را نیز کپی می‌کند. در طول این فرآیند، مقادیر در صورت نیاز به نوع جدیدی تبدیل می‌شوند.
106107
```js run
107108
let arr16 = new Uint16Array([1, 1000]);
108109
*!*
109110
let arr8 = new Uint8Array(arr16);
110111
*/!*
111112
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 بیت جا دهد(توضیخات در پایین)
113114
```
114115

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` خواهد بود:
116117
```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 (اندازه در بایت‌ها)
120121
```
121122

122-
5. Without arguments, creates an zero-length typed array.
123+
5. بدون آرگومان‌ها یک آرایه‌ی typed با طول صفر می‌سازد.
123124

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` دربرگیرنده نمی‌تواند وجود دشاته باشد؛ در نتیجه در همه‌ی این موارد بجز مورد اول(هنگامی که فراهم شده است) به طور خودکار ساخته می@شود.
125126

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`.
129130

130-
So, we can always move from one view to another:
131+
بنابراین، ما همیشه می‌توانیم از یک view به دیگری برویم:
131132
```js
132133
let arr8 = new Uint8Array([0, 1, 2, 3]);
133134
134-
// another view on the same data
135+
// دیگر در همان داده view یک
135136
let arr16 = new Uint16Array(arr8.buffer);
136137
```
137138

138139

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 بیتی
140146

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` یا انواع مشابه با مقدار واحد"
145148
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` در جاوااسکریپت وجود ندارد.
148150
149-
That's logical, as `Int8Array` is not an array of these individual values, but rather a view on `ArrayBuffer`.
151+
این موضوع منطقی است، زیرا `Int8Array` یک آرایه از این مقادیر مجزا نیست، بلکه یک view روی `ArrayBuffer` است.
150152
```
151153

152-
### Out-of-bounds behavior
154+
### رفتار خارج از محدوده
153155

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 اگر بخواهیم یک مقدار خارج از محدوده را در یک آرایه‌ی
155157

156-
For instance, let's try to put 256 into `Uint8Array`. In binary form, 256 is `100000000` (9 bits), 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 را در
157159

158-
For bigger numbers, only the rightmost (less significant) 8 bits are stored, and the rest is cut off:
160+
:برای اعداد بزرگتر، فقط 8 بیت سمت راست(بیت‌های کم‌ارزش‌تر) ذخیره می‌شود، و بقیه بیت‌ها حذف می‌شوند
159161

160162
![](8bit-integer-256.svg)
161163

162-
So we'll get zero.
164+
.در نتیجه صفر دریافت می‌کنیم
163165

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` را داریم
165167

166168
![](8bit-integer-257.svg)
169+
ذخیره می‌شود 2<sup>8</sup> به عبارت دیگر، باقی‌مانده عدد
167170

168-
In other words, the number modulo 2<sup>8</sup> is saved.
169-
170-
Here's the demo:
171+
:یک نمونه
171172

172173
```js run
173174
let uint8array = new Uint8Array(16);
174175
175176
let num = 256;
176-
alert(num.toString(2)); // 100000000 (binary representation)
177+
alert(num.toString(2)); // 100000000 (نمایش دودویی)
177178
178179
uint8array[0] = 256;
179180
uint8array[1] = 257;
@@ -182,9 +183,9 @@ alert(uint8array[0]); // 0
182183
alert(uint8array[1]); // 1
183184
```
184185

185-
`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 را ذخیره می‌کند. این رفتار ببرای پردازش تصویر مفید است.
186187

187-
## TypedArray methods
188+
## متدهای TypedArray
188189

189190
`TypedArray` has regular `Array` methods, with notable exceptions.
190191

0 commit comments

Comments
 (0)