JavaScript中的ArrayBuffer对象表示固定长度的二进制数据缓冲区。该isView()
对象的功能接受一个参数,并验证它是否为ArrayBuffer的视图(DataView,类型为数组)。如果是这样,则返回true,否则返回false。
其语法如下
arrayBuffer.isView(arg)
请尝试以下示例。
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var arrayBuffer = new ArrayBuffer(5); arrayBuffer = ["apple", "orange", "mango"]; var bool = ArrayBuffer.isView(new Int32Array()) document.write(bool); </script> </body> </html>
输出结果
true
以相同的方式,如果我们尝试通过传递除类型数组之外的对象或null值或undefined值来执行此函数,则此函数返回false。
<html> <head> <title>JavaScript Example</title> </head> <body> <script> var arrayBuffer = new ArrayBuffer(5); arrayBuffer = ["apple", "orange", "mango"]; var bool1 = ArrayBuffer.isView(new Int32Array()); var bool2 = ArrayBuffer.isView(); var bool3 = ArrayBuffer.isView(null); var bool4 = ArrayBuffer.isView(undefined); console.log(bool1); console.log(bool2); console.log(bool3); console.log(bool4); </script> </body> </html>
输出结果
True false false false