Array.isArray() เมธอดเป็นสากล มันสามารถทำงานได้ทุกที่ในขณะที่อินสแตนซ์ของ โอเปอเรเตอร์ไม่เป็นสากล ไม่สามารถทำงานได้ในสภาพแวดล้อมใหม่..
ไวยากรณ์-1
Array.isArray(array);
ไวยากรณ์-2
array instance of Array;
ในตัวอย่างต่อไปนี้ ซึ่งไม่มีการสร้างสภาพแวดล้อมใหม่ ทั้ง Array.isArrar() และ ตัวอย่าง ได้ผลผลิตเท่าเดิม
ตัวอย่าง
<html>
<body>
<script>
var a = [1,2,3,4,5];
document.write(Array.isArray(a));
document.write("</br>");
document.write((a instanceof Array));
document.write("</br>");
var b = {}
document.write(Array.isArray(b));
document.write("</br>");
document.write((b instanceof Array));
</script>
</body>
</html> ผลลัพธ์
true true false false
ตอนนี้เรามาลองสร้างสภาพแวดล้อมใหม่ หรือเฟรมใหม่เพื่อตรวจสอบว่า อินสแตนซ์ของ โอเปอเรเตอร์ทำงานที่นั่นหรือไม่
ในตัวอย่างต่อไปนี้ เฟรมใหม่จะถูกสร้างขึ้นโดยใช้ 'iframe '(คุณสมบัติเฟรมที่สร้างอาร์เรย์เหมือนวัตถุ) ต่อมาในอาร์เรย์เช่นวัตถุจะถูกสร้างขึ้นในเฟรมใหม่นั้นและส่งผ่านทั้งสองฟังก์ชัน ตั้งแต่ ตัวอย่าง ไม่เป็นสากล แต่ต้องใช้ที่ framed array ไม่ใช่อาร์เรย์จริงและส่งคืน false เป็นผลลัพธ์ ในขณะที่ Array.isArray() ส่งคืน จริง ดังแสดงในผลลัพธ์
ตัวอย่าง
<html>
<body>
<script>
var iframeE = document.createElement('iframe');
iframeE.style.display = "none";
document.body.appendChild(iframeE);
iframeArray = window.frames[window.frames.length - 1].Array;
var a = new Array(1,2,3,"hi",4, "hello");
var b = new iframeArray(1,2,3,4);
document.write(Array.isArray(a));
document.write("</br>");
document.write(a instanceof Array);
document.write("</br>");
document.write(Array.isArray(b));
document.write("</br>");
document.write(b instanceof Array);
</script>
</body>
</html> ผลลัพธ์
true true true false