Detailed explanation of typeof and instanceof

1. typeOf checks whether a variable is a variable of a basic type, and it returns a string. To be precise, determine whether a variable returns a string, a number, a boolean, an object, or undefined.
var a="zhangqian";  
var b=true;  
var c=10;  
var d;  
var e = null;  
var f=new Object();  
  
alert(typeof a); //string  
alert(typeof b); //boolean  
alert(typeof c); //number
alert(typeof d); //undefined  
alert(typeof e); //object  
alert(typeof f); //object  

2. instanceof mainly detects the reference type and returns a boolean value. The instanceof operator can be used to determine whether the prototype property of a constructor exists on the prototype chain of another object to be tested.
var array=new Array();  
var object=new Object();  
var regexp=new RegExp();  
function func(){};  
var func1 = new func ();  
  
alert(array instanceof Array);  //true  
alert(object instanceof Object);  //true  
alert(regexp instanceof RegExp);  //true  
alert(func1 instanceof func);  //true  


More related explanation links:
http://blog.csdn.net/myhahaxiao/article/details/6740542
http://blog.csdn.net/mevicky/article/details/50353881

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326584991&siteId=291194637