Three ways to determine if an object is an array

1. Use the instanceof operator

 

2. Array.isArray() (new method in ES5)

 

3. Use the native toString() method on Object.prototype to judge.

Instructions:

Object.prototype.toString.call(value)
  var a= {};
   var b= [];
   var frame=document.createElement("iframe"); // Create a frame 
  document.body.appendChild(frame);
   var c=window.frames[0].Array; // Get the Array constructor in the framework's global execution environment 
  var d= new c(); // Create an array in the framework's global execution environment d 
  console.log(Object.prototype.toString.call(a)); // [object Object] 
  console.log(Object.prototype.toString.call(b)); // [object Array] 
  console.log(Object.prototype.toString.call(d)); // [object Array]
  
  function Person() {
     this.name=name;
  }
  var n = new Person ();
  console.log(Object.prototype.toString.call(n));//[object Object]

This method cannot detect the function name of a non-native constructor, so any constructor defined will return [object Object].

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325378661&siteId=291194637