JS Array type detection

Source: Internet
Author: User

In strongly typed languages, array type detection is a very easy thing (typeof can be solved), and in the weak language JS data type is easily confused.

The common data types in JS are: Number, String, Boolean, Undefined, function, array, object, and null. The following first Test with typeof:

var k = 9; Console.log (typeof K);  //  Number

The detection number can be detected with TypeOf.

var k = "string"; Console.log (typeof K);  // string

The detection string can be detected with TypeOf.

var true ; Console.log (typeof K);   // Boolean

The detection Boolean can be detected with TypeOf.

var U;console.log (typeof u);  // undefined

Detection undefined can also be detected with TypeOf.

var function () {};console.log (typeof F);   // function

Function can also be detected with TypeOf.

var NULL ; Console.log (typeof T);   // Object var array = [];console.log (typeof//objectvar obj =  {};console.log (typeof obj);    // Object

Using typeof to detect Null,array and object detection is an object. It appears that these three data structures need to be handled in a special way.

function type (o) {     returnnull)? "Null": (typeof  o)  }

Special handling of NULL is performed first, in order to see if array and object have other properties to judge.

var array = []; var toString =//[object Array]var obj = {}; Console.log ( Tostring.apply (obj)); // [Object Object]

OK, by detecting other data types: regexp and date can also be detected in this way:

var regex =/^\s+/var toString = Object.prototype.tostring;console.log (tostring.apply (regex));   // [Object RegExp] var New date (); Console.log (Tostring.apply (Date));   // [Object Date]

Finally, let's write a generic function to detect the JS data type:

functionTypeOf (e) {var_tostring =Object.prototype.toString; var_type = {        "Undefined": "Undefined",        "Number": "Number",        "Boolean": "Boolean",        "String": "String",        "Function": "Function",        "[Object RegExp]": "Regex",        "[Object array]": "Array",        "[Object Date]": "Date"    }    return_type[typeofE] | | _type[_tostring.call (e)] | | E? ' object ': ' null ');}

I see some other ways to detect data types in Zepto.js, and you can refer to:

varToString = ({}). tostring;//quite ingenious.  functionIsfunction (value) {returnTostring.call (value) = = "[Object Function]" }functionIsObject (value) {returnValueinstanceofObject}//Returns false for True,new object simultaneous arguments for objects that are defined by literal values and new//The prototype chain of the constructor of the new object simultaneous parameter has no isprototypeof property, while the constructor's __proto__ object has a prototype chain. functionIsplainobject (value) {varKey, ctor//returns if it is not an object type.     if(Tostring.call (value)!== "[Object Object]")return false     //gets the properties or objects in the prototype chain of the objectctor = (isfunction (value.constructor) &&Value.constructor.prototype)//There is no object or attribute in the prototype chain, or there is no property isprototypeof, and returns false    if(!ctor | |!hasownproperty.call (ctor, ' isprototypeof '))return false     for(Keyinchvalue); returnKey = = = Undefined | |Hasownproperty.call (value, key)}functionIsArray (value) {returnValueinstanceofArray}functionLikearray (obj) {//class array, not a real array, is an object that has a member type of "Length"    return typeofObj.length = = ' Number ' }

JS Array type detection

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.