JavaScript to determine whether a variable is an array, function, or object type, javascript Array

Source: Internet
Author: User

JavaScript to determine whether a variable is an array, function, or object type, javascript Array

Array

In ECMAScript5, Array. isArray is a native Method for judging arrays. IE9 and later support. Considering compatibility, you can use Object. prototype. toString. call (obj) = '[object Array]' in browsers without this method.

Copy codeThe Code is as follows:
Var isArray = Array. isArray | function (obj ){
Return Object. prototype. toString. call (obj) = '[object Array]';
}

Function

The simplest and best-performing method is typeof obj = 'function '. Considering the bugs in Some browsers, the most reliable method is Object. prototype. toString. call (obj) = '[object Function]'.

Copy codeThe Code is as follows:
Var isFunction = function (obj ){
Return Object. prototype. toString. call (obj) = '[object Function]';
}
If (typeof /./! = 'Function' & typeof Int8Array! = 'Object '){
IsFunction = function (obj ){
Return typeof obj = 'function ';
}
}

Object

In JavaScript, complex types are objects and functions are objects. If typeof is used for the preceding two objects, 'object' and 'function' are obtained respectively '. In addition, the null value must be excluded because typeof null also produces 'object '.

Copy codeThe Code is as follows:
Var isObject = function (obj ){
Var type = typeof obj;
Return type = 'function' | type = 'object '&&!! Obj;
}

The above is all the content in this article. I hope you will like it.

Related Article

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.