Determine the functions of the JS data type

 Function JudgetYpe (CHANGE) {if (arguments.length == 0) {return '0'; // None parameter incoming} if (change === null) {Return 'null'} if (change === undefined && arguments.length> 0) {return 'undefined'} if (change instanceof function) {return 'function'} if (change instanceof Array) {return 'arry'} if ( change instanceof Number || typeof change == 'number') {return 'number'} if (change instanceof String || typeof change == 'string') {return 'string'} if (change instanceof Boolean || typeof change = = 'boolean') {return 'boolean'}}   
PS: Next, JS is judged to judge various data type

Knowing JS knows that there is a typeof to determine the various data types, there are two ways: TypeOf XXX, Typeof (XXx) The following examples:

typeof 2 output number typeof null output object typeof {} output object typeof [] output object typeof (function () { }) outputs the output function typeof undefined undefined typeof ‘222’ output the output string typeof true boolean
   it contains the five data types inside js number string boolean undefined object and function type function 
to see where you are sure to ask: how I went to distinguish objects, arrays, and null it

then we use another weapon:? Object.prototype.toString .call

it is a primary object prototype spread function, used to more accurately distinguish between data types.

Let’s try this play Italian children:

var gettype = Object.prototype.toString ( ‘aaaa’) output [object String] (2222) output [object Number] (true)Output [object boolean] (undefined) output [object undefined] (null) output [Object Null] ({}) Output [Object Object] ([]) Output [Object Array ] (function () {}) output [object function]
  Seeing this, we have solved the problem.  Constructor can also determine the data type: 

”. Constructor == String [] .constructor == array Var obj = new object () obj.constructor == Object

Actually there are many types of type judgments in JS

[Object HTMLDiveElement]

DIV object, [ Object htmlbodyElement] body object
, [Object Document] (IE) or
 [Object HTMLDocument] (Firefox, Google)  ...... Decision of various DOM nodes, these things It will be used when we write a plugin.  
The method that can be encapsulated is as follows:

Var gettype = Object.Prototype.tostring var utility = {isobj: function (o) {return (o) == ” [object object] “;}, isarray: function (o) {return (o) ==” [object array] “;}, isnull: function (o) {return (o) ==” [object null] “;}, isdocument: function () {return (o) ==” [Object Document] || [object htmldocument];} ……..} This method of acquiring has a simple way:

VAR TYPE = (function () {var type = {} var typearr = [‘string’, ‘object’, ‘number’, ‘arrness’, ‘undefined’, ‘function’, ‘null’, ‘symbol’]; for (var i = 0; i
   Call method: 
Type.isfunctionION () {}) type.isobject ({}) . . . . .
TYPE.Is …..

 The above is the judgment JS data type for Xiaobic introduction. Function instance explanation, I hope everyone can help, if you have any questions, please leave a message, the small package will reply to everyone!  < typeArr.length; i++) {
          (function(name) {
            type['Is' + name] = function(obj) {
              return == '[object ' + name + ']';
        return type;
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment