JavaScript - TypedArray of() 方法



JavaScript TypedArray 的 of() 方法根据指定数量的参数创建一个新的类型化数组。它返回一个包含作为参数提供的元素的类型化数组实例。参数必须与结果类型化数组的元素具有相同类型

如果我们传递的参数值为'undefined''empty',它将创建一个元素值为0的新类型化数组。

这是一个静态方法,这意味着它可以在不创建 TypedArray 对象实例的情况下调用(如 TypedArray.of())。TypedArray 对象可以是 TypedArray 的任何子类,例如 Uint8Array、Int16Array 等。

语法

以下是 JavaScript TypedArray of() 方法的语法:

TypedArray.of(element1, element2, /*..., */ elementN)

参数

此方法接受多个相同类型的参数,即:'element1','element2',....'elementN'。具体说明如下:

  • element1,..., elementN - 用于创建新类型化数组的元素。

返回值

此方法返回新类型化数组的实例。

示例

示例 1

如果传递空('') 字符串,它将被转换为数字0 并添加到类型化数组中。

在下面的程序中,我们使用 JavaScript TypedArray of() 方法创建一个具有指定空('') 参数值的新类型化数组。

<html>
<head>
   <title>JavaScript TypedArray of() Method</title>
</head>
<body>
   <script>
      //Unit8Array
      let T_array = Uint8Array.of('');
      document.write("The new typed array: ", T_array);
      document.write("<br>Length of typed array: ", T_array.length);
   </script>
</body>
</html>

输出

上述程序创建一个新的类型化数组 [0]。

The new typed array: 0
Length of typed array: 1

示例 2

如果我们向此方法传递多个相同类型的参数,它将使用这些参数值创建一个新的类型化数组。

以下是 JavaScript TypedArray of() 方法的另一个示例。我们使用此方法使用指定值'10','20','30','40' 和 '50' 创建一个新的类型化数组。

<html>
<head>
   <title>JavaScript TypedArray of() Method</title>
</head>
<body>
   <script>
      //Unit16Array
      let ele1 = '10';
      let ele2 = '20';
      let ele3 = '30';
      let ele4 = '40';
      let ele5 = '50';
      document.write("Element1 = ", ele1);
      document.write("<br>Element2 = ", ele2);
      document.write("<br>Element3 = ", ele3);
      document.write("<br>Element4 = ", ele4);
      document.write("<br>Element5 = ", ele5);
      let T_array = Uint16Array.of(ele1, ele2, ele3, ele4, ele5);
      document.write("<br>The new typed array: ", T_array);
      document.write("<br>Length of typed array: ", T_array.length);
   </script>
</body>
</html>

输出

执行上述程序后,它将返回新类型化数组的实例,如下所示:

Element1 = 10
Element2 = 20
Element3 = 30
Element4 = 40
Element5 = 50
The new typed array: 10,20,30,40,50
Length of typed array: 5

示例 3

如果我们将'undefined' 作为参数传递给of() 方法,它将创建一个元素值为 0 的新类型化数组。

在下面的示例中,我们使用类型化数组of() 方法使用指定的参数值'undefined' 创建一个新的类型化数组。

<html>
<head>
   <title>JavaScript TypedArray of() Method</title>
</head>
<body>
   <script>
      //Unit8Array
      let T_array = Uint8Array.of(undefined);
      document.write("The new typed array: ", T_array);
      document.write("<br>Length of typed array: ", T_array.length);
   </script>
</body>
</html>

输出

执行上述程序后,它将创建一个元素值为 0 的新类型化数组。

The new typed array: 0
Length of typed array: 1

示例 4

如果此方法的值不是有效的类型化数组构造函数,则of() 方法将抛出'TypeError' 异常。例如,如果尝试在数字、字符串或对象上调用该方法,则会得到 TypeError。

<html>
<head>
   <title>JavaScript TypedArray of() Method</title>
</head>
<body>
   <script>
      try {
         let T_array = "Hello".of(1, 2, 3);
         document.write("The new typed array: ", T_array);
         document.write("<br>Length of typed array: ", T_array.length);
      } catch (error) {
         document.write(error);
      }
   </script>
</body>
</html>

输出

上述程序将抛出 'TypeError' 异常:

TypeError: "Hello".of is not a function
广告
© . All rights reserved.