JavaScript is to determine whether an object is an array

Source: Internet
Author: User

The data type in JS:

2 major categories
Primitive type: The data type where the value is stored locally on the variable
5 kinds: Number String Boolean undefined null
Number:8bytes round-off error and rounding
String: 1byte per English character
2bytes per Kanji character
Once the content is created, it cannot be changed
Boolean:true false
Reference type: value does not guarantee the existence of a variable local data type

The variable vs database in JS
Files on the hard disk in memory
Temporary and lasting.
Only one copy is stored on the client server side

Generally use typeof to determine the data type: The following > Note the last one

Console.log (typeof//stringconsole.log (typeof// number Console.log (typeof//objectconsole.log (typeof(function//  functionconsole.log (typeof//Object

Why not array ? How do you tell if an object is a object array type?

typeof can detect basic types including undefined, string, number, Boolean, but it is not reliable for the detection object.

Not only objects in Array,javascript, including Date, String, Boolean, Number, object, Function, Array, REGEXP, Error using TYPOF will only return "object".

The following method is given in 4:

//if the current browser's array constructor object does not contain IsArray    if(!Array.isarray) {    //Add a IsArray method to the array constructorarray.isarray=function(obj) {//determine if obj is an array of type 4            //1. If the array.prototype is in the prototype chain of obj              //return Array.prototype.isPrototypeOf (obj);            //2. Using instanceof: Syntax: obj instanceof constructor name            //return obj instanceof Array;            //instance: Each concrete object created by a constructor            //3. Constructor properties for prototype objects            //return obj.constructor==array;            //4. Using the ToString method of the object prototype            /*call,apply call, replacing the object that invokes the method at the instant of the calling method*/            returnObject.prototype.toString.apply (obj) = = "[Object Array]"; }    //}

Note: (from https://segmentfault.com/q/1010000000130605)

It is not a reliable way to use instanceof or constructor to detect arrays.

If the array to be detected comes from an array of IFRAME, both instanceof and contructor are invalidated.

Because each IFRAME has its own execution environment, objects that are instantiated across a frame do not share the prototype chain with each other.

var iframe = document.createelement (' iframe ');     Document.body.appendChild (IFRAME);      = Window.frames[window.frames.length-1]. Array;      var New // [A]      instanceof // false      // false

JavaScript is to determine whether an object is 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.