In JavaScript, the most reliable way to determine what kind of built-in type an object's value belongs to is through the Object.prototype.toString method.
Example:
var array=[1,2,3];object.prototype.tostring.call (array); // "[Object Array]" var obj={name: ' Tom '};object.prototype.tostring.call (obj); // "[Object Object]" var string= ' Hi '; Object.prototype.toString.call (string); // "[Object String]" var num=1; Object.prototype.toString.call (num); // "[Object number]"
There are other ways, of course, but it's a bit troublesome.
such as TypeOf, but the typeof is not measured whether the object being detected is an object or an array, because object contains an array, such as:
var array=[1,2,3]; var obj={name: ' Tom '}; typeof Array; // "Object" typeof obj; // "Object"
You can see that the return is "object".
With instanceof, you can accurately detect JS types like Object.prototype.toString. such as:
var o=[]; instanceof Array); // true instanceof Object); // true var f=function() {} instanceof function); // true instanceof Object); //
Summary: Simple to use simple and straightforward, so with Object.prototype.toString.call (obj) this way is good.
Type of JS Detection object