Typeof: operator that detects data types, returnedThe result is a lowercase string of type

The result is a string, and the contents of the string prove that the value is of what type

  1. Underlying data typesIn addition totypeof nullis'object'Outside,typeofAre allOwn data type
  2. Reference data typeCan be divided into:Object data typeandFunction data type
  3. Object data typeCan be divided into: {}Common object typeAnd []An array of$/, / ^regular.The date objectEtc.
  4. Object data typeandnullthetypeofAre all'object'
  5. Function data typethetypeofAre all"Function"

Limitations:

  1. Typeof NULL is not 'null' but 'object'Because:nullAlthough it is a separate data type, it originally meansNull object pointerFor browserstypeofThey do when they’re testedExamine it as an object
  2. usetypeofThere’s no way to break it downAn array oforregularBecause the return result is'object'

Interview questions:

typeof typeof[] = >'string'
//1, typeof [] = "object"
//2, typeof 'object' = 'string'
Copy the code
	typeof 12= >'number'

	var num=13;
	typeof num => 'number'
	typeof 'aaa'= >'string'
	typeof true= >'boolean'
	typeof null= >'object'    / / typeof bug
	typeof undefined= >'undefined'

	typeof {aaa:'aaa'} = >'object'
	typeof[] = >'object'
	typeof/ ^ $/ = >'object'
	typeof function (){} = >'function'
Copy the code

XX instanceof YYTo check whether the current instance belongs to a class

function Fn(){}var f=new Fn();

console.log(f instanceof Fn)        //=>true

[] instanceof Array    true
[] instanceof Object   true

console.log([] instanceof Array)    //=>true
console.log($/ / ^ instanceof Fn)     //=>false

[] instanceof Object                //true$/ / ^instanceof Object              //true$/ / ^instanceof Array               //false
[] instanceof Array                 //true$/ / ^instanceof RegExp              //true

function aaa(){}
console.log(aaa instanceof Function)  //true
Copy the code

Manually implement instanceof(Only object datatype instanceof uppercase function names can be detected)

Instanceof is used to determine whether an object is an instanceof another object (constructor). Instanceof looks up the prototype chain until null returns false if it is not an instanceof the later object, and true otherwise

function instanceofMy(obj, constructor) {
  // the constructor must be a function
  if (typeof constructor! = = 'function') throw new Error('instance error')
  if (! obj || (typeofobj ! = ='object' && typeofobj ! = ='function')) return false// Get the prototype objectlet proto = constructor.prototype/ / ifobjThe prototype object is notnull
  while (obj.__proto__) {
    if (obj.__proto__ === proto) return true
    obj = obj.__proto__
  }
  return false
}

console.log(instanceofMy(() = > {}, Function)) // true
Copy the code

XXX. Constructor: Gets the constructor for the current instance

var test=1;
//var test=[1];
//var test='1';

if (test.constructor==Array)
{
    console.log("This is an Array");
}
if (test.constructor==Boolean)
{
    console.log("This is an Boolean");
}
if (test.constructor==Date)
{
    console.log("This is an Date");
}
if (test.constructor==String)
{
    console.log("This is an String");
}
if (test.constructor==Number)
{
    console.log("This is an Number");
}

var test=[1];
console.log(test.constructor == Object); //false
Copy the code
function employee(name,job,born) {
    this.name=name;
    this.job=job;
    this.born=born;
}

var bill=new employee("Bill Gates"."Engineer".1985);
console.log(bill.constructor);
/ / the result
function employee(name,job,born) {
    this.name=name;
    this.job=job;
    this.born=born;
}
Copy the code

Object. The prototype. ToString. Call: access to the current instance belongs to the class information

Object. The prototype. The toString method, the judgment of an Object to belong to what kind of built-in types.

The options are null, string, Boolean, number, undefined, array, function, object, date, math

Judge basic types:

Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call("abc");// "[object String]"
Object.prototype.toString.call(123);// "[object Number]"
Object.prototype.toString.call(true);// "[object Boolean]"
Copy the code

Determine the native reference type:

//** Function type **
Function fn(){
  console.log (" test "); }Object.prototype.toString.call(fn); // "[object Function]"

//** Date type **
var date = new Date(a);Object.prototype.toString.call(date); // "[object Date]"

//** Array type **
var arr = [1.2.3];
Object.prototype.toString.call(arr); // "[object Array]"

//** regular expression **
var reg = /[hbc]at/gi;
Object.prototype.toString.call(reg); // "[object RegExp]"

Object.prototype.toString.call({})
//"[object Object]"

// Determine the native JSON object
var isNativeJSON = window.JSON && Object.prototype.toString.call(JSON);
console.log(isNativeJSON);// If the output is "[object JSON]", JSON is native; otherwise, it is not.

//** Custom type **
function Person(name, age) {
    this.name = name;
    this.age = age;
}
var person = new Person("Rose".18);
Object.prototype.toString.call(person); // "[object object]" // Person is an instance of the Person class
Copy the code

For the type that we define ourselves

For example, function Fn() {} requires the well-known toStringTag to be set

Fn.prototype[Symbol.toStringTag] = 'Fun'

Object.prototype.toString.call(new Fn()) // "[object Fun]"
Copy the code