Type detection in JavaScript

Source: Internet
Author: User

Article Starter: http://www.cnblogs.com/sprying/p/4349426.html

This article lists the general JS type detection method, is the construction of JS knowledge system of a small piece, this article is my early summary.

In JS, there are 5 basic data types undefined, Null, Boolean, String, number (including Nan) Nan and any type of value are unequal, including Nan;isnan used to determine whether the value is a Nan type Second, the type judgment 1. Isfinite (number) is not infinite, if it is not true, if it is Nan, either positive or negative infinity, or a non-numeric type returns false

2. When used by the typeof operator, a space or typeof (Param) Returns the value Stringnumberbooleanundefinedfunctionobject null also returns an object according to the above, judging the type can be as follows:
var obtaintype = function (o) {     var t;     if (o = = = null) return "NULL";     else if (o!== o) return "NaN";     else if ((t = typeof O)!== ' object ') return t;}
The null, NaN string number Boolean undefined function can be recognized. Only object is left above, such as array recognition, custom type recognition 3. The recognition of native types such as arrays can be used as follows
function Obtaintype (type) {    return function (obj) {        return Object.prototype.toString.call (obj) = = = "[Object" + t Ype + "]"    }}var IsObject = Istype ("Object") var isstring = Istype ("String") var IsArray = Array.isarray | | Istype ("Array") var isfunction = Istype ("Function")

4. Custom type judgment
/** * Returns the name of the function, possibly an empty string, is not a function, returns null */function.prototype.getname = function () {    if ("name" in this) return this.name;
   
    return this.name = this.tostring (). Match (/function\s* ([^ (]*) \ (/) [1];};
   

Both the native type and the custom type of object can be judged, so
/** * return: null NaN undefined string Number Boolean * function Array string Object (including some custom types) custom type */var Obtaintype =functi     On (o) {/** * get parameter Type * Object Direct amount, Object.create, custom constructor class attribute are all object;    * Identification of native types (built-in constructors and host objects) */function classof (obj) {return Object.prototype.toString.call (obj). Slice (8,-1);  }/** * Returns the name of the function, possibly an empty string, not a function, returns null */Function.prototype.getName = function () {if ("name" in this)        return this.name;    return this.name = This.tostring (). Match (/function\s* ([^ (]*) \ (/) [1];    };    var t, c, N;    Handling Null Value Special case if (o = = = null) return "NULL";    Nan: Not equal to its own value if (o!== o) return "NaN";    Identify the primitive value type and function, undefined if ((t = typeof O)!== "Object") return t;    Identify the primitive type if ((c = classof (o))!== "Object") return C; Returns the custom type constructor name if (o.constructor && typeof o.constructor = = = "function" && (n = o.constructor.    GetName ())) return n; return "Object";};

5.

var strobj = new String (' abc '); typeof Strobj//"Object" Obtaintype (strobj)//"string"

Third, the other 1. DOM element to determine if (Dom.nodetype) {... Dom ...} if (dom.createelement) 2. Types such as JQuery determine $ (' #aa ') instanceof jquery//is not supported across multiple windows and frames sub-page 3. if (a) A is null undefined 0 "" Nan when automatically converted to false general recommended notation
BADIF (Name!== ") {    //...} stuff ...} GOODIF (name) {    //...} stuff Badif (Collection.length > 0) {    //...} stuff ...} Goodif (collection.length) {    //...} stuff ...}

Type detection in JavaScript

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.