<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta  name="viewport" content="width=device-width, Initial-scale =1.0"> <title>Document</title> </head> <body> </body> </ HTML > <script Typeof [value] => String, including corresponding data type limitations: Typeof null -> "object" typeof null -> "object" typeof null -> "object" typeof null -> "object" Typeof detects the value of the object type corresponding to the original value, and results in a string "object". The typeof test data type is based on the binary value stored in the underlying computer. It considers all objects starting with "000" and null to be zero. Instanceof [value] instanceof [Ctor] => true/false The value of the original value type cannot be detected, but the object format instance corresponding to the original value can be detected. But because the prototype chain direction can be arbitrarily changed, so the final detection result may not be accurate; Principle: according to the prototype chain detection; The result is TRUE if only the currently detected constructor (its prototype object) appears in the instance's prototype chain; If Object. Prototype is not found, then FALSE; F instanceof Fn -> Fn[Symbol. HasInstance](f) 4. Object. The prototype. ToString. Call ([value]) this is the only one in JS test data type, please do not have any defect in the "the most accurate, in addition to performance than typepf slightly less then a diu diu, "=>"[object] Number/String/Boolean/Null/Undefined/Symbol/BigInt/object/Function/RegExp/Array/Math/GeneratorFunction... (prototype is what what types of detected object behind like what)] "most on the prototype of the inner class has a toString method, generally is converted to a string, but the object. The prototype. The toString is testing data types, The return value contains information about the constructor to which it belongs... Object.prototype.toString.call([value]) 1. Why "call"? ([]).toString calls array. prototype toString, which converts to a string ({}). ToString calls Object. Prototype toString, which checks the datatype of this method. Whose type is test "- > we need to get the Object. The prototype. ToString implementation, but also change the this - > ({}). ToString. Call (10) = >" [Object Number] "2. What are the rules for detecting return values? Are usually returned to the current instance constructor type information they belong to But if the instance objects have Math [Symbol toStringTag] = "Math" = > Object. The prototype. ToString. Call (Math) = > "[Object Math]" */ // 4.Object.prototype.toString.call([value]) /* class Fn{ // constructor(){ // this[Symbol.toStringTag]="Fn" // } [Symbol.toStringTag]="Fn"; } let f =new Fn; console.log(Object.prototype.toString.call([Fn])); //"[object Function]" console.log(Object.prototype.toString.call([f])); //"[object object]" ? */ / GeneratorFunction constructor =[],n=10,m=new Number(10); console.log(arr.constructor===Array); //=>true console.log(arr.constructor===RegExp); //=>false console.log(arr.constructor===Object); Console. log(n.constructor===Number); console.log(n.constructor===Number); //=>true console.log(m.constructor===Number); //=>true /* function Fn(){} let f =new Fn; console.log(f.constructor===Fn); //=>true console.log(f.constructor===Object); //=>false */ /*function Fn(){} Fn.prototype={}; Constructor let f =new Fn; console.log(f.constructor===Fn); //=>fale console.log(f.constructor===Object); / / / / / = > true * 2. Instanceof / * let arr = [10, 30]; console.log(arr instanceof Array); //=>true console.log(arr instanceof RegExp); //=>false console.log(arr instanceof Object); //=>true console.log(new Number(1) instanceof Number); //=>true console.log(1 instanceof Number); //=>false */ /* // class Fn{ // } // Fn.prototype=Array.prototype; // let f=new Fn; // function Fn() { // } // Fn.prototype = Array.prototype; // let f = new Fn; // console.log(f instanceof Array); */ */ function Fn() {}// Fn[symbol.hasinstance](f)=function (){} ES6 syntax does!! Class Fn{// New syntax rewrite can modify static[symbol.hasinstance](){console.log('OK'); if(Array.isArray(obj)) return true; return false; } } let f = new Fn; Let arr=[10,20,30] console.log(f instanceof Fn); //=>true console.log(arr instanceof Fn); //=>true // console.log(Fn[Symbol.hasInstance](f)); Const instance_of = function instance_of(obj, Ctor) { if (Ctor === null) throw new TypeError('Right-hand side of intanceof is not an object'); let type = typeof Ctor; if (! /^object|function)$/i.test(type)) throw new TypeError('Right-hand side of intanceof is not an object'); if (! /^function)$/i.test(type)) throw new TypeError('Right-hand side of intanceof is not an object'); if (! Ctor.prototype) throw new TypeError('Function has non-object prototype in instanceof check'); let proto = Object.getPrototypeOf(obj); while (proto) { if (proto === Ctor.prototype) return true; proto = Object.getPrototypeOf(proto); } return false; }; console.log(instance_of([], Array)); //=>true console.log(instance_of([], RegExp)); //=>false console.log(instance_of(1, Number)); //=>true By default, the browser checks the prototype console.log(instance_of(Symbol(), Symbol)); //=>true console.log(instance_of([], {})); // </script> </script> </script>Copy the code