Original http://blog.csdn.net/mashangyou/article/details/24036233
You may know that the type of object in JS can be judged by typeof. Look at the situation below.
<script> alert (typeof 1),//number alert (typeof "2"),//string alert (typeof []);//object alert (typeof {"Name": "Zhuhui"})//object</script>
From the above we can see that the array and the ordinary objects are judged by the TypeOf is object, but now we have this requirement, we have to determine exactly which object is this object (such as Array object, Date object, regular Expression object, other custom object, Dom objects and so on) what then? In fact, JS has a method can be prepared to determine the
Object.prototype.toString.call
varType=function(v) {returnObject.prototype.toString.call (v); }; Alert (Type (NULL));//[Object Null]Alert (type (undefined));//[Object Undefined]Alert (Type (1));//[Object number]Alert (Type (true));//[Object Boolean]Alert (Type ("2"));//[Object String]Alert (type ([+]));//[Object Array]Alert (Type ({"Name": "Zhuhui"});//[Object Object]alert (type);//[Object Function]Alert (Type (NewDate ()));//[Object Date]Alert (Type (/^\d+$/));//[Object Regexp]
For a more in-depth explanation of this method, please refer to
Http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html
The following is the previous project package to determine the common data type JS
/** * type.js data type detection function * @author Zhu Hui * @email [email protected] * @version 0.1*/(function(window, undefined) {Xjo= Window.xjo | |{plugin: {}}; Xjo.type= {}; //type auxiliary function for detecting v varType =function(v) {returnObject.prototype.toString.call (v); }; /** * If the array object type is true if it is not, return false * @namespace Xjo.type * @method IsArray * @param {any} v detected variable * @return {Boolean} result*/Xjo.type.isArray=function(v) {returnType (v) = = = ' [Object Array] '; }; /** * is the parameter manager arguments returns TRUE if it is not, returns false * @param {any} v detected variable * @return {Boolean}*/xjo.type.isArguments=function(v) {returnV.callee! =undefined; }; /** * Whether the iteration sequence contains an array with arguments if it is true if it is not, return false * @param {any} v detected variable * @return {Boolean} */xjo.type.isIterable=function(v) {returnXjo.type.isArray (v) | |xjo.type.isArguments (v); }; /** * Whether empty objects are null and undefined and the length of the array is 0 or an empty string ("") if it is true if it is not, return false * @param {any} v detected variable * @param {Bool EAN} Allowblank [optional] default false empty string is considered null object whereas an empty string is not considered an empty object * @return {Boolean}*/Xjo.type.isEmpty=function(V, allowblank) {returnv = =NULL|| v = = Undefined | |(Xjo.type.isArray (v)&&!v.length) | | (!allowblank? v = = = ':false); }; /** * is a string type if it is true if it is not, return false * @param {any} v detected variable * @return {Boolean}*/xjo.type.isString=function(v) {return typeofv = = = ' String '; }; /** * is a numeric type (number and not a plus or minus infinity) if it is true if it is not, return false * @param {any} v detected variable * @return {Boolean} */Xjo.type.isNumber=function(v) {return typeofv = = ' number ' &&Isfinite (v); }; /** * Whether it is a Boolean type if it returns true if it is not, returns false * @param {any} v detected variable * @return {Boolean}*/Xjo.type.isBoolean=function(v) {return typeofv = = ' Boolean '; }; /** * is the function type if it is true if it is not, return false * @param {any} v detected variable * @return {Boolean}*/xjo.type.isFuntion=function(v) {returnType (v) = = = ' [Object Function] '; }; /** * is the object type * @param {any} v detected variable * @return {Boolean}*/Xjo.type.isObject=function(v) {return!! V && type (v) = = = ' [Object Object] '; }; /** * Whether it is a date type if it is true if it is not, return false * @param {any} v detected variable * @return {Boolean}*/xjo.type.isDate=function(v) {returnType (v) = = = ' [Object Date] '; }; /** * is a regular expression type if it is true if it is not return false * @param {any} v detected variable * @return {Boolean}*/Xjo.type.isRegexp=function(v) {returnType (v) = = ' [Object RegExp] '; }; /** * If the original data type is true if it is not, return false * @param {any} v detected variable * @return {Boolean}*/xjo.type.isPrimitive=function(v) {returnXjo.type.isString (v) | | Xjo.type.isNumber (v) | |Xjo.type.isBoolean (v); }; /** * Returns the string form of the data type <br> * Numeric type: "Number" <br> * Boolean Type: "Boolean" <br> * String Type: "String" <br> * Array type: "Array" <br> * Date Type: "Date" <br> * Regular expression type: "RegExp" <br> * function type: "Func tion "<br> * Object Type:" Object "<br> * parameter Manager type:" Arguments "<br> * other type:" Unknow "* @param {any } v the variable being detected * @return {String}*/Xjo.type.type=function(v) {varresult = "Unknow"; if(Xjo.type.isNumber (v)) {result= "Number"; } if(Xjo.type.isBoolean (v)) {result= "Boolean"; } if(xjo.type.isString (v)) {result= "string"; } if(Xjo.type.isArray (v)) {result= "Array"; } if(Xjo.type.isDate (v)) {result= "Date"; } if(Xjo.type.isRegexp (v)) {result= "RegExp"; } if(Xjo.type.isFuntion (v)) {result= "function"; } if(Xjo.type.isObject (v)) {result= "Object"; } if(Xjo.type.isArguments (v)) {result= "Argumetns"; } returnresult; }; xjo.plugin["Jo/type"] =true;}) (window);
The specific type of judging object in JS