Three ways to determine if an object is not an array

Source: Internet
Author: User
Tags object object

1. Using the instanceof operator

2, Array.isarray () (ES5 new method)

3. Use the native ToString () method on Object.prototype to determine.

How to use:

Object.prototype.toString.call (value)
  varA={}; varb=[]; varFrame=document.createelement ("iframe");//Create a FrameDocument.body.appendChild (frame); varC=window.frames[0]. Array;//gets the array constructor in the framework global execution Environment  varD=NewC ();//Create an array d in the framework Global execution EnvironmentConsole.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]    functionPerson () { This. name=name; }  varn=NewPerson (); Console.log (Object.prototype.toString.call (n));//[Object Object]

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

Three ways to determine if an object is not an array

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.