如何检查一个值是否为JavaScript中的`SharedArrayBuffer`视图?

huangapple go评论84阅读模式
英文:

How do I check if a value is a `SharedArrayBuffer` view, in Javascript?

问题

ArrayBuffer.isView() 静态方法 用于检查它是否是 ArrayBuffer 视图,例如 TypedArray 对象或 DataView

要检查是否是 SharedArrayBuffer 视图,你应该如何操作?

自从引入 SharedArrayBuffer 以来已经过了相当长的时间,然

英文:

The ArrayBuffer.isView() static method checks if it's one of the ArrayBuffer views, such as TypedArray objects or a DataView.

How do you check if it's a view of a SharedArrayBuffer?

It's been a quite long time since SharedArrayBuffer was introduced, yet oddly, nobody's talking about the SharedArrayBuffer counterpart of the ArrayBuffer.isView method. Or, is it that I can use ArrayBuffer.isView to check for SharedArrayBuffer views as well?

I've read https://stackoverflow.com/q/15251879/4510033 but this question seems to be specifically focusing on ArrayBuffers, not SharedArrayBuffers.

答案1

得分: 0

你可以在视图的 .buffer 属性上使用 instanceof

view.buffer instanceof SharedArrayBuffer

如果你不确定对象是否是任何缓冲区上的视图,请首先使用 ArrayBuffer.isView(view)

英文:

You can use instanceof on the .buffer property of the view:

view.buffer instanceof SharedArrayBuffer

If you are not certain whether the object is a view on any buffer at all, use ArrayBuffer.isView(view) first.

huangapple
  • 本文由 发表于 2023年5月7日 17:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定