0

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?

1 Answers1

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
  • 1
    The 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