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 gettype.call ( ‘aaaa’) output [object String] gettype.call (2222) output [object Number] gettype.call (true)Output [object boolean] gettype.call (undefined) output [object undefined] gettype.call (null) output [Object Null] GetType.call ({}) Output [Object Object] GetType.call ([]) Output [Object Array ] gettype.call (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 gettype.call (o) == ” [object object] “;}, isarray: function (o) {return gettype.call (o) ==” [object array] “;}, isnull: function (o) {return gettype.call (o) ==” [object null] “;}, isdocument: function () {return gettype.call (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 …..

Summary
 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.prototype.toString.call(obj) == '[object ' + name + ']';
            }
          })(typeArr[i]);
        }
        return type;
})();                     
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment