DataView的getInt16()函数在指定位置获取并返回一个带符号的16位整数。
其语法如下
dataView.getInt16();
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var arrayBuffer = new ArrayBuffer(20); var dataView = new DataView(arrayBuffer); dataView.setInt16(1, 3225); document.write(dataView.getInt16(1)); </script> </body> </html>
输出结果
3225
对于此函数,您不能传递浮点值,如果尝试这样做,则将其视为整数值。
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var arrayBuffer = new ArrayBuffer(20); var dataView = new DataView(arrayBuffer); dataView.setInt16(1, 564.22); document.write(dataView.getInt16(1)); </script> </body> </html>
输出结果
564
如果数据视图中什么也没有存储,如果仍然尝试获取数据,此函数将返回0。
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var arrayBuffer = new ArrayBuffer(20); var dataView = new DataView(arrayBuffer); dataView.setInt16(1); document.write(dataView.getInt16(1)); </script> </body> </html>
输出结果
0