The first two days to participate in the front-end of the telephone interview, which asked the array type of judgment, although previously seen in this aspect of the content, but the time has been a bit vague, the answer is not very good, now on the relevant content to do a review and summary.
Let's start by answering the question: how can I tell if an object is an array?
There are several ways to determine whether an object is an array method:
One, Array.isarray () function
This can be done in ECMAScript5 by Array.isarray ():
Array.isarray ({}); // false Array.isarray ([]); // true
Second, instanceof
The instanceof operator is used to determine whether a variable is an instance of an object and can only be used in a simple case:
instanceof Array //trueinstanceof array //false
instanceof problem: In a Web browser, there may be multiple windows or multiple forms, each with its own JS environment and its own global object, each with its own set of constructors, so that one form object cannot be an instance of a constructor in another form. Confusion in forms does not occur frequently.
Third, prototype and call
Object.prototype.toString.call ([]) = = = "[Object Array]" //true Object.prototype.toString.call ({}) = = = "[Object Array]" //false
Iv. Constructor
Constructor returns the constructor of an object
[].constructor==array //true{}.constructor==array ///false
Note: Using instanceof and Construtor to determine the array, the array being judged must be declared on the current page. For example, when an array is declared in a sub-page and its value is assigned to a variable of the parent page, the variable is judged: variable. Constructor==array; returns false at this time.
Cause: The array belongs to the reference data, only the reference address is passed during the pass, and each page array native object's referenced address is different, that is, the parent page's array is not equal to the sub-page array.
Other types of judging methods are similar to arrays, and here is a list of the return values for instanceof and constructor:
| Variable |
Variable. constructor |
typeof variable |
| [] |
Array |
Object |
| {} |
Object |
Object |
| var f=function () {} |
Fuction |
function |
| "A String" |
String |
String |
| 88 |
Number |
Number |
| True |
Boolean |
Boolean |
| var a |
/ |
Undefined |
| Null |
/ |
Object |
| New User () |
User |
Object |
Review of JS array type judgment and data type judgment