In JavaScript, the judgment variable is an array, function, or object type. _javascript Tips

Source: Internet
Author: User

Array

In ECMAScript5, Array.isarray is the primary method of judging arrays, IE9 and above support. With compatibility in mind, in browsers without this method, you can use Object.prototype.toString.call (obj) = = ' [Object Array] ' instead.

Copy Code code as follows:

var IsArray = Array.isarray | | function (obj) {
return Object.prototype.toString.call (obj) = = ' [Object Array] ';
}

Function

The simplest and best way to do this is to typeof obj = ' function '. Given the bugs that exist in some version browsers, the most plausible approach is Object.prototype.toString.call (obj) = = ' [Object Function] '.

Copy Code code 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

Complex types are objects in JavaScript, and functions are objects. Using typeof for the above 2 people, you can get ' object ' and ' function ' respectively. In addition, the case of null values is excluded, because typeof null is also ' object '.

Copy Code code as follows:

var isobject = function (obj) {
var type = typeof obj;
return type = = ' function ' | | Type = = = ' object ' &&!! Obj
}

The above is the entire content of this article, I hope you can enjoy.

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.