Phương thức Array.of() của JavaScript dùng để tạo instance mảng mới với các biến làm giá trị tham số.
Cú pháp:
Array.of(elements....)
Trong đó, elements là các giá trị làm tham số.
Triển khai Array.of() trong JavaScript:
Ví dụ 1
<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Click the button to display the values in Console</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
function display() {
console.log(Array.of(10, 20, 30, 40, 50));
}
</script>
</body>
</html>
Kết quả

Click "Result" và xem Console –

Ví dụ 2
<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<p>Get the student names with top 5 ranks...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
function display() {
console.log(Array.of("Kevin", "Tom", "Ryan", "Jacob", "Jack"));
}
</script>
</body>
</html>
Kết quả

Click "Result" và xem Console –
