There are generally four methods to determine object types (primitive and complex) :
- typeof
- instanceof
- constructor
- Object.prototype.toString.call()
Note: to test the integrity of the, sequentially, it is recommended to use a final Object. The prototype. ToString. Call
First, the typeof
Typeof can detect number, string, Boolean, undefined, symbol, object and function;
- Commonly used to distinguish basic data types.
- Unable to distinguish objects from arrays (Typeof determines that both objects and arrays return object)
typeof 0; //number;
typeof true; //boolean;
typeof undefined; //undefined;
typeof "hello world" //string;
typeof function(){}; //function;
typeof null; //object
typeof {}; //object;
typeof []; //object
Copy the code
let s = Symbol(a);typeof s //"symbol"
s instanceof Symbol //false
Copy the code
Second, the instanceof
The instanceof operator is used to check whether the constructor’s prototype property exists on the prototype chain of an instance object.
- Generally used to judge arrays and objects;
var o = { };
var a = ['reg'.'blue'];
o instanceof Object; // true
o instanceof Array; // false
a instanceof Array; // true
a instanceof Object; // true
Copy the code
- You cannot distinguish between the basic types String and Boolean, unless it is a string object and a Boolean object.
var c = 'abc';
c instanceof String; //false
var d = new String(a); dinstanceof String //true
Copy the code
- This is a bit special when the new instance changes the function’s prototype
// Define the constructor
function C(){}
var o = new C();
o instanceof C; // true because object.getProtoTypeof (o) === c.protoType
C.prototype = {}; // Change the constructor's prototype
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false, c. protoType is overwritten, but o's prototype object points to the previous prototype object
Copy the code
Third, the constructor
- Variables of any type except undefined and null can be typed using constructor.
var o={};
o.constructor == Object //true
var arr = [];
arr.constructor == Array //true
arr.constructor == Object //false
var n = true;
n.constructor == Boolean //true
var num1 = 1;
var num2 = new Number(a); num1.constructor ==Number //true
num2.constructor == Number //true
var str = 'hello world';
str.constructor == String //true
Copy the code
- Note, however, that the constructor attribute can be modified, resulting in incorrect results being detected
function Person(){}function Student(){ }
Student.prototype = new Person();
var John = new Student();
John.constructor == Student; // false
John.constructor == Person; // true
Copy the code
Four, the Object. The prototype. ToString. Call () (recommended)
Recommended use of this method, the type of detection is very comprehensive oh!
Object.prototype.toString.call(null) //"[object Null]"
Object.prototype.toString.call(undefined) //"[object Undefined]"
Object.prototype.toString.call(123) //"[object Number]"
Object.prototype.toString.call('str') //"[object String]"
Object.prototype.toString.call(true) //"[object Boolean]"
Object.prototype.toString.call({}) //"[object Object]"
Object.prototype.toString.call([]) //"[object Array]"
Copy the code
Determine the data type, using arrays and objects as examples:
function typeObj(obj){
if(type=='[object Array]') {return 'Array';
}elseif(type=='[object Object]'){
return 'Object'; }}Copy the code
The above method can also be encapsulated as follows:
function type(obj) {
return Object.prototype.toString.call(obj).replace(/\[object\s|\]/g."");
}
function isArray(list) {
return type(list) === "Array";
}
function isObject(obj) {
return type(obj) === "Object";
}
function isString(str) {
return type(str) === "String";
}
Copy the code