Inferred object detail type in JS

Source: Internet
Author: User

You may know that the Inferred object type in JS can be inferred using typeof.

Look at the following situation

<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 object are inferred from the TypeOf is object, but now we have this demand, we have to understand that this object is the detail of which object (such as Array object, Date object. An object that is a regular form. Other self-defined objects, DOM objects, etc.) what about that? In fact, there is a method in JS that can be prepared to infer

Object.prototype.toString.call
           var type=function (v) {           return Object.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 (new Date ()));//[object Date]           alert (Type (/^\d+$/));//[object Regexp]
For more in-depth instructions on this approach, please refer to http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html

Here are the JS that used to do project encapsulation inference common data types

/** * type.js Data type check 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 var type = function (v) {return Object.prototype.toString.call (v);        };     /** * Whether the array object type is assumed to return true if it is not, returns false * @namespace Xjo.type * @method IsArray * @param {any} v the variable being measured    * @return {Boolean} result */Xjo.type.isArray = function (v) {return type (v) = = = ' [Object Array] ';    }; /** * is the parameter manager arguments assuming that it returns true if it is not, returns false * @param {any} V is detected variable * @return {Boolean} */Xjo.type    . isarguments = function (v) {return v.callee! = undefined;    };    /** * Whether the iteration sequence includes an array with arguments is assumed to return true if it is not to return false * @param {any} V is detected variable * @return {Boolean} */    xjo.type.isIterable = function (v) {return Xjo.type.isArray (v) | | xjo.type.isArguments (v);    }; /** * is empty object null and undefined and the length of the array is 0 or nullThe string ("") is assumed to return true if it is not to return false * @param {any} V is detected variable * @param {Boolean} allowblank [optional] default false empty string think empty object and null  String does not feel empty object * @return {Boolean} */xjo.type.isEmpty = function (V, allowblank) {return v = = = NULL | | v = = =        Undefined | |        (Xjo.type.isArray (v) &&!v.length) | |    (!allowblank v = = = ": false);    };  /** * Whether the string type is assumed to be the return true assumes that it is not the return false * @param {any} V is detected variable * @return {Boolean} */xjo.type.isString    = function (v) {return typeof v = = = ' String ';    };    /** * is a numeric type (number and is not a plus or minus infinity) assuming that it is true to return false * @param {any} V is detected variable * @return {Boolean} */            Xjo.type.isNumber = function (v) {return typeof v = = = ' Number ' && isfinite (v);    }; /** * Whether a Boolean type is assumed to return true if it is not, returns false * @param {any} V is detected variable * @return {Boolean} */Xjo.type.isBoole    an = function (v) {return typeof v = = = ' Boolean ';    }; /** * Whether the function type is assumed to be true if it is not returnedFalse * @param {any} v the variable being detected * @return {Boolean} */xjo.type.isFuntion = function (v) {return type (    V) = = = ' [Object Function] ';    }; /** * is the object type * @param {any} v the variable being detected * @return {boolean} */xjo.type.isObject = function (v) {RE Turn!!    V && type (v) = = = ' [Object Object] ';    };  /** * Whether a date type is assumed to return true assumes that it is not return false * @param {any} V is detected variable * @return {boolean} */xjo.type.isDate =    Function (v) {return type (v) = = = ' [Object Date] ';    }; /** * Whether or not the statement type is assumed to return true if it is not, returns false * @param {any} V is detected variable * @return {Boolean} */Xjo.type.isReg    Exp = function (v) {return type (v) = = ' [Object RegExp] ';    }; /** * Whether the original data type is assumed to return true if it is not, returns false * @param {any} V is detected variable * @return {Boolean} */Xjo.type.isPrimi        tive = function (v) {return xjo.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 table type: "RegExp" <br> * function Type: "Function" <br> * Object Type: "Object"     <br> * Parameter manager type: "Arguments" <br> * other type: "Unknow" * @param {any} v detected variable * @return {String}        */Xjo.type.type = function (v) {var result = "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";    } return result;    }; xjo.plugin["Jo/type"] = true;}) (window);



Inferred object detail type in JS

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.