typeof

let s = 'string';
let n = 1;
let boo = true;
let nul = null;
let und = undefined;
let fun = function(){};
let obj = {
  name: 'huihui'.age: 8
};
let arr = [1.2.3];
let sym = Symbol(1);
console.log(typeof(s));    // 'string'
console.log(typeof(n));    // 'number'
console.log(typeof(boo));  // 'boolean'
console.log(typeof(nul));  // 'object'
console.log(typeof(und));  // 'undefined'
console.log(typeof(fun))   // 'function'
console.log(typeof(obj))   // 'object'
console.log(typeof(arr))   // 'object'
console.log(typeof(sym))     // 'symbol'
Copy the code

Why does typeof NULL return object

When JS stores variables at the bottom, it stores their type information in the lowest 1-3 bits of the machine code

  • 000: object
  • 010: floating point number
  • 100: string
  • 110: Boolean
  • 1: the integer

Object.prototype.toSting.call()

let s = 'string';
let n = 1;
let boo = true;
let nul = null;
let und = undefined;
let fun = function() {};
let obj = {
    name: 'huihui'.age: 8
};
let arr = [1.2.3];
let sym = Symbol(1);

console.log(Object.prototype.toString.call(s));    // [object, String]
console.log(Object.prototype.toString.call(n));    // [object, Number]     
console.log(Object.prototype.toString.call(boo));  // [object, Boolean]
console.log(Object.prototype.toString.call(nul));  // [object, Null]
console.log(Object.prototype.toString.call(und));  // [object, Undefined]
console.log(Object.prototype.toString.call(fun));  // [object, Function]
console.log(Object.prototype.toString.call(obj));  // [object, Object]
console.log(Object.prototype.toString.call(arr));  // [object, Array]
console.log(Object.prototype.toString.call(sym));  // [object, Symbol]
Copy the code

instanceof

The main function of instanceof is to determine whether an instance belongs to a certain type

let person = function() {}
let notice = new person();
notice instanceof person    // true
Copy the code

Instanceof can also determine whether an instance is an instanceof its parent or ancestor type

let person = function(){}
let programmer = function(){}
programmer.prototype = new person();
let notice = new programmer();
console.log(notice instanceof person)    // true
console.log(notice instanceof programmer)    // true
Copy the code

Implementation principle of Instanceof

function new_instance_of(leftVaule, rightVaule) { 
    let rightProto = rightVaule.prototype; // Take the prototype value of the right expression
    leftVaule = leftVaule.__proto__; // Take the __proto__ value of the left expression
    while (true) {
    	if (leftVaule === null) {
            return false;	
        }
        if (leftVaule === rightProto) {
            return true;	
        } 
        leftVaule = leftVaule.__proto__ 
    }
}
Copy the code

In fact, the main implementation principle of Instanceof is as long as the right variable’s prototype is on the left variable’s prototype chain.

  • Analyze the Object instanceof Object
leftValue = Object.__proto__ = Function.prototype;
rightValue = Object.prototype;
// First judgmentleftValue ! = rightValue; leftValue =Function.prototype.__proto__ = Object.prototype;
// Second judgment
leftValue = rightValue
/ / return true
Copy the code
  • Function instanceof Function
leftValue = Function.__proto__ = Function.prototype;
rightValue = Function.prototype;
// First judgment
leftValue = rightValue;
/ / return true
Copy the code
  • Analyze Foo instanceof Foo
leftValue = Foo.__proto__ = Function.prototype;
rightValue = Foo.prototype;
// First judgment
leftValue = rightValue;
// Second judgment
leftValue = Function.prototype.__proto__ = Object.prototype leftValue ! = rightValue// The third judgment
leftValue = Object.prototype.__proto__ = nullleftValue ! = rightValue/ / returns false
Copy the code
  • Analyze Foo instanceof Object
leftValue = Foo.__proto__ = Function.prototype;
rightValue = Object.prototype;
// First judgmentleftValue ! = rightValue;// Second judgment
leftValue = Function.prototype.__proto__ = Object.prototype
leftValue = rightValue
/ / return true
Copy the code
  • Analyze Foo instanceof Function
leftValue = Foo.__proto__ = Function.prototype;
rightValue = Function.prototype;
// First judgment
leftValue = rightValue;
/ / return true
Copy the code

conclusion

If you want to determine the specific typeof an object, you can use instanceof, but instanceof may not be accurate. For example, an array can be judged as an Object by instanceof. So we want to more accurately judge the type of Object instances, can take the Object. The prototype. ToString. Call method.