4 types of identification methods in JavaScript, including javascript4
The details are as follows:
1. typeof
Output: a string in lower case.
[Function]
[A] can recognize standard types (recognize Null as an object)
[B] The specific object type cannot be identified (except for Function)
[Instance]
console.log(typeof "jerry");//"string"console.log(typeof 12);//"number"console.log(typeof true);//"boolean"console.log(typeof undefined);//"undefined"console.log(typeof null);//"object"console.log(typeof {name: "jerry"});//"object"console.log(typeof function(){});//"function"console.log(typeof []);//"object"console.log(typeof new Date);//"object"console.log(typeof /\d/);//"object"function Person(){};console.log(typeof new Person);//"object"
2. Object. prototype. toString
[Output] string format of [object Data Type]
[Function]
[A] Identifying standard and built-in Object Types
[B] user-defined types cannot be recognized
[Constructor]
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();}
[Instance 1]
console.log(Object.prototype.toString.call("jerry"));//[object String]console.log(Object.prototype.toString.call(12));//[object Number]console.log(Object.prototype.toString.call(true));//[object Boolean]console.log(Object.prototype.toString.call(undefined));//[object Undefined]console.log(Object.prototype.toString.call(null));//[object Null]console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]console.log(Object.prototype.toString.call(function(){}));//[object Function]console.log(Object.prototype.toString.call([]));//[object Array]console.log(Object.prototype.toString.call(new Date));//[object Date]console.log(Object.prototype.toString.call(/\d/));//[object RegExp]function Person(){};console.log(Object.prototype.toString.call(new Person));//[object Object]
[Instance 2]
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();}console.log(type("jerry"));//"string"console.log(type(12));//"number"console.log(type(true));//"boolean"console.log(type(undefined));//"undefined"console.log(type(null));//"null"console.log(type({name: "jerry"}));//"object"console.log(type(function(){}));//"function"console.log(type([]));//"array"console.log(type(new Date));//"date"console.log(type(/\d/));//"regexp"function Person(){};console.log(type(new Person));//"object"
3. constructor
[Output] function data type () {[native code]} or function custom type (){}
[Function]
[A] recognition of standard types, built-in object types, and custom types
[B] If undefined or null is not recognized, an error is returned.
[Constructor]
function type(obj){ var temp = obj.constructor.toString(); return temp.replace(/^function (\w+)\(\).+$/,'$1');}
[Instance 1]
Console. log ("jerry "). constructor); // function String () {[native code]} console. log (12 ). constructor); // function Number () {[native code]} console. log (true ). constructor); // function Boolean () {[native code]} // console. log (undefined ). constructor); // error // console. log (null ). constructor); // error console. log ({name: "jerry "}). constructor); // function Object () {[native code]} console. log (function (){}). constructor); // function Function () {[native code]} console. log ([]). constructor); // function Array () {[native code]} console. log (new Date ). constructor); // function Date () {[native code]} console. log (/\ d /). constructor); // function RegExp () {[native code]} function Person () {}; console. log (new Person ). constructor); // function Person (){}
[Instance 2]
Function type (obj) {var temp = obj. constructor. toString (). toLowerCase (); return temp. replace (/^ function (\ w + )\(\). + $/, '$ 1');} console. log (type ("jerry"); // "string" console. log (type (12); // "number" console. log (type (true); // "boolean" // console. log (type (undefined); // error // console. log (type (null); // error console. log (type ({name: "jerry"}); // "object" console. log (type (function () {})); // "function" console. log (type ([]); // "array" console. log (type (new Date); // "date" console. log (type (/\ d/); // "regexp" function Person () {}; console. log (type (new Person); // "person"
4. instanceof
Output: true or false
[Function]
[A] Identifying built-in object types, custom types, and parent types
[B] if the standard type is not recognized, false is returned.
[C] If undefined or null is not recognized, an error is returned.
[Instance]
Console. log ("jerry" instanceof String); // falseconsole. log (12 instanceof Number); // falseconsole. log (true instanceof Boolean); // false // console. log (undefined instanceof Undefined); // error // console. log (null instanceof Null); // error console. log ({name: "jerry"} instanceof Object); // trueconsole. log (function () {} instanceof Function); // trueconsole. log ([] instanceof Array); // trueconsole. log (new Date instanceof Date); // trueconsole. log (// \ d/instanceof RegExp); // truefunction Person () {}; console. log (new Person instanceof Person); // trueconsole. log (new Person instanceof Object); // true
The above is a detailed description of the four types of identification methods in JavaScript.