How can I tell if an object is an array type?

Source: Internet
Author: User

The typeof operator is useful to determine the type of operand in the previous assignment of variables and assignment of objects, and the result is as follows:

Alert (typeof 1);                Returns the string "number"  alert (typeof "1");              Returns the string "string"  alert (typeof true);             Returns the string "Boolean"  alert (typeof {});               Returns the string "Object"  alert (typeof []);               Returns the string "Object"  alert (typeof function () {});     Returns the string "function"  alert (typeof null);             Returns the string "Object"  alert (typeof undefined);        Returns the string "undefined"

Where the result of typeof {} and typeof [] is object, then the question comes, how can I tell by TypeOf whether an object is an array type?

Objects are objects, arrays are objects, and everything in JS is object, and it is clear that the simple typeof operator is not able to achieve the goal, we have to change the method.

--------------------------------------------------------------------------------------------------------------- ------------

Here are some ways to try:

1, starting from the prototype, Array.prototype.isPrototypeOf (obj);

The isPrototypeOf () method is used to determine if the array is in the prototype chain of obj, and if so, returns True, otherwise false.

2, can also start from the constructor, obj instanceof Array

Let's talk about the difference between TypeOf and Instanceof.

Both can be used to determine the variable, typeof will return the basic type, such as the beginning of the article, we can easily use

typeof A! = ' undefined ' determines the existence of a variable. and instanceof only returns a Boolean value, so let's try it, and the result is as follows:

The above method can also use the object constructor property, because in JS each object has a constructor attribute, which is also commonly used to determine the type of unknown objects.

For example: typeof arr = = "Object" && arr.constructor = = Array; First Judge is the object to further judge.

Is that the way it's OK? Don't be happy so early, there will always be a pit.

In some cross-frame pages of an array, using this method may not be so smooth, because arrays created in different frameworks do not share their prototype properties with each other.

The problem is always solved, is there a more precise method? = = Indeed there is!

3. Call the ToString () method across the prototype chain, depending on the class attribute of the object.

Explain, in JS, once an object is created, inside will carry the creation of the object's type name, can be called nickname, once created cannot be modified. So how do we get to this nickname?

JS provides the ToString method in the Call object prototype, Object.prototype.toString.call (obj);

So with this method, getting the object name can be obtained like this:

4, Array.isarray () method.

In fact, JS has provided a way to determine whether the array type, as follows:

Array.isarray ([1, 2, 3]);  Truearray.isarray ({foo:123}); Falsearray.isarray (' Foobar ');   Falsearray.isarray (undefined);  False

The difference between IsArray and instanceof is compared in MDN, and when Array.isarray () is unavailable, the MDN makes the following patch, stating that it is recommended to use the third method described above Object.prototype.toString.call (obj).

Do you have enough of the above four methods?

-----------------------------------------------------End----------------------------------------------------------------- ----------

  

How can I tell if an object is not an array type?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.