Home  >  Article  >  Web Front-end  >  Summary of distinguishing between Object and Aarry methods in JS

Summary of distinguishing between Object and Aarry methods in JS

高洛峰
高洛峰Original
2017-02-28 14:18:531138browse

1. Frequently encountered problems:

When judging the type of an object in JS, typeof is usually used. At this time, the problem arises, because typeof() returns when identifying an array. object, so judging whether an object is an array in JS

requires some special processing methods. The six processing methods summarized by me are introduced below.

2. Straight to the point

To determine whether an object is an array during development, it is recommended to use the following function:

function isArray(obj){
  if(Array.isArray){
    return Array.isArray(obj);
  }else{
   return Object.prototype.toString.call(obj)==="[object Array]";
  }
}


The above function is convenient for people who are eager to solve problems. I will describe the six methods in detail below, because the examiner may need a comprehensive knowledge of you during the interview;

3. Detailed explanation of the six solutions:

(1) Method 1: Use the toString method

Try to convert the variable into a string representing its type by calling the toString() method. This method is feasible for real arrays; when the parameter object is converted to string

returns [object Arguments], the conversion will fail; in addition, the conversion will also fail for the object class containing a numeric length attribute.

The method is as follows:

nbsp;html>

 
 Array的判断方法
 
 
 
 
 
 

The result is as shown:

Summary of distinguishing between Object and Aarry methods in JS

Note: It is recommended to use "===" for equals instead of "==" for equals, because it is more efficient!

(2) Method 2: Through isArray:

Using Javascript 1.8.5 (ECMAScript 5), the variable name .isArray() can achieve this purpose, provided that this function is supported. In fact, isArray is the encapsulation of

method one.

The usage is very simple:

Array.isArray(obj); //obj is the object to be detected

Returns true or false, if true, it is an array

(3) Method 3: Judge through the instanceof operator,

Note: The left side of the instanceof operator is the child object (the object to be tested), and the right side is the parent constructor (here is Array),

That is: child object instanceof parent constructor

instance: instance: any object created with new constructor () is called an instance of the constructor

I’ve been confused for a long time, so it’s better to look at the code directly:

nbsp;html>

 
 
 
 
 
 
 Document
 
 
 
 
 

The running results are as follows:

Summary of distinguishing between Object and Aarry methods in JS

(4) Use the isPrototypeOf() function

Principle: Detect whether an object is the prototype of Array (or is in the prototype chain, not only the direct parent object, but also all parent objects on the entire prototype chain can be detected)

Usage: parent.isPrototypeOf(child) to detect whether parent is the prototype of child;

It should be noted that the function implemented by the isPrototypeOf() function is very similar to the instancof operator;

Specific code:

Array.prototype.isPrototypeOf(arr) //true means it is an array, false means it is not an array

(5)Use the constructor

Specific code:

nbsp;html>

 
 
 
 
 
 
 Document
 
 
 
	
 

The result is as shown in the figure

Summary of distinguishing between Object and Aarry methods in JS

(6) Use typeof(object ) + type name combined judgment:

The code is as follows:

nbsp;html>

 
 
 
 
 
 
 Document
 
 
 
 
 

The result is as follows:

This method actually has limitations Sex, some students may crack it at once, that is, what if

What if this attribute is accidentally defined in the object

var obj = {'concat':'Teast me?'};

Summary of distinguishing between Object and Aarry methods in JS

The above summary of the six methods of distinguishing Object and Aarry in JS is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more articles related to the summary of the JS method of distinguishing Object and Aarry, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn