How do I make the content of an ArrayBuffer
immutable in Javascript? The Object.freeze()
method does not seem to be working. Is there any method to achieve it? Would it be even possible?
Asked
Active
Viewed 501 times
0

Константин Ван
- 12,550
- 7
- 61
- 73
-
it already is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer "You cannot directly manipulate the contents of an ArrayBuffer" – Robbie Milejczak Jan 20 '18 at 19:44
-
What are you trying to achieve? – guest271314 Jan 20 '18 at 19:46
-
@RobbieMilejczak I'm sorry but I know that already. What I am asking in this question is, **the way to prevent an `ArrayBuffer` from being manipulated THROUGH _Views_** such as _Typed arrays_ and `DataView`s. So that I can fill an `ArrayBuffer` with its initial data, and then _freeze_ it. – Константин Ван Jan 20 '18 at 19:47
-
@guest271314 I'm trying to prevent an `ArrayBuffer` from being manipulated through _Views_. – Константин Ван Jan 20 '18 at 19:53
-
sheesh okay well ya didn't say that bud – Robbie Milejczak Jan 20 '18 at 19:58
-
@RobbieMilejczak I apologize if it sounded rude. I never meant to be rude. – Константин Ван Jan 20 '18 at 20:00
1 Answers
1
You can convert the ArrayBuffer
to an immutable string as a data URI
defined using const
const reader = new FileReader;
reader.onload = () => { const ab = reader.result // `ab` cannot be changed }
reader.readAsDataURL(new ArrayBuffer(123));
See Is it possible to delete a variable declared using const??

guest271314
- 1
- 15
- 104
- 177
-
Oh, an _immutable copier_ solution. The original `ArrayBuffer` would be no longer able to be accessed, and only the immutable objects clone it out.. Seems great, but why did you removed your `Promise.resolve()` solution? – Константин Ван Jan 20 '18 at 20:11
-
The `Promise.resolve()` approach needs to be adjusted. How is the `ArrayBuffer` declared? – guest271314 Jan 20 '18 at 20:18
-
Correction first: "why did you *remove". -- The `ArrayBuffer` can be declared (created) in any ways. – Константин Ван Jan 20 '18 at 20:22
-
1The linked answer describes why `const` `let` declarations cannot be deleted with `delete`. Where the `ArrayBuffer` is not declared using `const` or `let` you can try to `delete` the variable. Else, use the string approach – guest271314 Jan 20 '18 at 20:26