4 types of recognition methods in JavaScript _javascript tips

Source: Internet
Author: User
Tags object object

The specific contents are as follows:

1.typeof

String form for "output" first letter lowercase

function

[A] can recognize the standard type (recognize null as Object)
[b] does not recognize specific object types (except for function)

Instance

Console.log (typeof "Jerry");/"string"
Console.log (typeof);//"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

string form of output [object data type]

function

[A] can recognize standard types and built-in object types
[b] does not recognize custom types

"Construct 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 ())//[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);/"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 (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] can recognize standard types, built-in object types, and custom types
[b] cannot recognize undefined, NULL, error

"Construct 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);//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+) \ (\). +$/, ' $ ';
}
Console.log (Type ("Jerry"));//"string"
console.log (type);/"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 (new date);//"Date"
console.log (Type (/\d/));//"regexp"
function person () {};
Console.log (type (new person));//"Person"

4.instanceof

' Output ' true or false

function

[A] can recognize built-in object types, custom types, and their parent types
[b] does not recognize the standard type and returns false
[C] cannot recognize undefined, NULL, error

Instance

Console.log ("Jerry" instanceof String);//false
Console.log (instanceof number);//false
Console.log (True instanceof Boolean);//false
//console.log (undefined instanceof undefined);//error
//console.log (null instanceof Null);//Error
Console.log ({name: "Jerry"} instanceof Object);//true
Console.log (function () {} instanceof Function);//true
Console.log ([] instanceof Array);//true
Console.log (new date instanceof date); True
Console.log (/\d/instanceof RegExp);//true
function Person () {};
Console.log (new person instanceof person),//true
console.log (new person instanceof Object);//true

The above is a detailed description of JavaScript in 4 types of identification methods, I hope you like.

Related Article

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.