JavaScript Array.of() 函数

javascriptweb developmentfront end technologyobject oriented programming更新于 2024/8/15 21:00:17

JavaScript 的 Array.of() 方法用于创建一个新的数组实例,并以变量作为参数值。

语法如下 −

Array.of(elements....)

上面,元素是作为参数值的值。

现在让我们在 JavaScript 中实现 Array.of() 方法 −

示例

<!DOCTYPE html>
<html>
<body>
   <h2>Demo Heading</h2>
   <p>点击按钮在控制台中显示值</p>
   <button onclick="display()">结果</button>
   <p id="test"></p>
   <script>
      function display() {
         console.log(Array.of(10, 20, 30, 40 ,50));
      }
   </script>
</body>
</html>

输出

单击"结果"并检查控制台中的输出 −

示例

<!DOCTYPE html>
<html>
<body>
   <h2>演示标题</h2>
   <p>获取排名前 5 位的学生姓名...</p>
   <button onclick="display()">结果</button>
   <p id="test"></p>
   <script>
      function display() {
           console.log(Array.of("Kevin", "Tom", "Ryan", "Jacob", "Jack"));
      }
   </script>
</body>
</html>

输出

单击"结果"按钮并在控制台中检查结果 −


相关文章


有用资源