4 types of recognition methods in JavaScript

Source: Internet
Author: User

"1" typeof
"Output" lowercase string as the first letter
function
[A] can recognize a standard type (recognizes null as Object)
[b] does not recognize specific object types (except function)

Instance

Console.log (typeof"Jerry");//"string"Console.log (typeof12);//"Number"Console.log (typeof true);//"Boolean"Console.log (typeofundefined);//"undefined"Console.log (typeof NULL);//"Object"Console.log (typeof{Name: "Jerry"});//"Object"Console.log (typeof function(){});//"function"Console.log (typeof[]);//"Object"Console.log (typeof NewDate);//"Object"Console.log (typeof/\d/);//"Object"functionPerson () {};console.log (typeof Newperson);//"Object"

"2" Object.prototype.toString
String form of "Output" [Object data Type]
function
[A] can identify standard types and built-in object types
[b] does not recognize a custom type
"Construction Method"

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 (NewDate));//[Object Date]Console.log (Object.prototype.toString.call (/\d/));//[Object RegExp]functionPerson () {};console.log (Object.prototype.toString.call (Newperson));//[Object Object]

"Instance 2"

functiontype (obj) {returnObject.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 (NewDate));//"Date"Console.log (Type (/\d/));//"RegExp"functionPerson () {};console.log (Type (Newperson));//"Object"

"3" constructor
"Output" function data type () {[native code]} or function custom type () {}
function
[A] can recognize standard types, built-in object types, and custom types
[b] cannot recognize undefined, NULL, error
"Construction Method"

function type (obj) {    var temp = obj.constructor.toString ();     return temp.replace (/^function (\w+) \ (\). +$/, ' $ ');}

"Instance 1"

Console.log (("Jerry"). Constructor);//function String () {[native code]}Console.log ((+). constructor);//function Number () {[native code]}Console.log ((true). constructor);//function Boolean () {[native code]}//Console.log ((undefined). constructor);//Error//Console.log ((null). constructor);//ErrorConsole.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 ((NewDate). constructor);//function Date () {[native code]}Console.log ((/\d/). constructor);//function RegExp () {[native code]}functionPerson () {};console.log (Newperson). constructor);//function Person () {}

"Instance 2"

functiontype (obj) {vartemp =obj.constructor.toString (). toLowerCase (); returnTemp.replace (/^function (\w+) \ (\). +$/, ' $ ');} Console.log (Type ("Jerry"));//"string"Console.log (Type (12));//"Number"Console.log (Type (true));//"Boolean"//console.log (Type (undefined));//Error//console.log (type (null));//ErrorConsole.log (Type ({name: "Jerry"}));//"Object"Console.log (Type (function(){}));//"function"Console.log (Type ([]));//"Array"Console.log (Type (NewDate));//"Date"Console.log (Type (/\d/));//"RegExp"functionPerson () {};console.log (Type (Newperson));//"Person"

"4" instanceof
"Output" true or False
function
[A] can identify built-in object types, custom types, and their parent types
[b] does not recognize the standard type and will return false
[C] cannot recognize undefined, NULL, will error

Instance

Console.log ("Jerry"instanceofString);//falseConsole.log (12instanceofnumber);//falseConsole.log (true instanceofBoolean);//false//console.log (undefined instanceof undefined);//Error//console.log (null instanceof NULL);//ErrorConsole.log ({name: "Jerry"}instanceofObject);//trueConsole.log (function(){}instanceofFunction);//trueConsole.log ([]instanceofArray);//trueConsole.log (NewDateinstanceofDate);//trueConsole.log (/\d/instanceofREGEXP);//truefunctionPerson () {};console.log (NewPersoninstanceofperson);//trueConsole.log (NewPersoninstanceofObject);//true

4 types of recognition methods in JavaScript

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.