About THE JS judgment data type, yes, but it seems not completely, in the interview is always confused by some strange questions.

But with this article, mom won’t have to worry about being asked about data types in interviews anymore.

First blood

Interviewer: Can you tell us how JS determines data types?

I: Basic data types can be judged by typeof, and complex data types can be judged by instanceof, which is mainly realized by judging whether the former is an instance object of the latter. At the same time can also use the Object. The prototype. ToString. Call () method and constructor constructor to determine the data type.

Interviewer: Oh? Do you know how Instanceof determines if the former is an instantiation of the latter, and can implement it yourself?

Me :(write yourself? Excuse me, are you polite? Fortunately, I was prepared and not afraid at all.

First of all, an instanceof A is mainly searched layer by layer on the prototype chain of A. If a prototype can be found, it means that A is the instantiated object of A. If no A. protoType is found, then a is not an instantiated object of A. How do you know where to find the head? Because all objects are traced up the prototype chain, the last Object must be NULL, and the next-to-last Object is Object. So we can say that all objects are instantiations of Object.

For example, if we create an array of objects, let’s look at its prototype chain:

let arr = new Array()
arr.__proto__ === Array.prototype // true
arr.__proto__.__proto__ === Object.prototype // true
arr.__proto__.__proto__.__proto__ === null // true
Copy the code

With that in mind, we can write an Instanceof method by hand.

function myInstanceof(left,right){
    if(typeofleft ! = ='object' || left === null) {return false;
    }
    let leftProto = left.__proto__
    let rightProto = right.prototype
    while(true) {if(leftProto === null) {return false
        }
        if(leftProto === rightProto){
            return true
        }
        leftProto = leftProto.__proto__
    }
}
Copy the code

Double kill

Interviewer: right, that you have just said Object. The prototype. ToString. The call () how to determine the data type? What prototype and Call are used for.

Me :(tricky, but I can still do it)

First of all, why Object. Prototype? For things like arrays and strings, they overwrite toString()

console.log('123'.toString()); / / 123
console.log([1.2.3].toString()); / / 1, 2, 3
Copy the code

So we just use the Object. The prototype. The toString () this kind of insurance to write. This method returns a string of the form ‘[object XXX]’ based on this, after which XXX is the type of this object. If called directly, it is the type of Object and returns the value ‘[Object Object]’.

If we want to determine other types of objects, we can change the type of the object by changing the this object. How to change the reference of this? We can add a call method to the end of the method.

    Object.prototype.toString.call(' ');// [object String]
    Object.prototype.toString.call(1);// [object Number]
    Object.prototype.toString.call(true);// [object Boolean]
    Object.prototype.toString.call(undefined);// [object Undefined]
    Object.prototype.toString.call(null);// [object Null]
    Object.prototype.toString.call({}) ; // [object Object]
    Object.prototype.toString.call([]) ; // [object Array]
    Object.prototype.toString.call(new Function());// [object Function]
    Object.prototype.toString.call(new Date());// [object Date]
    Object.prototype.toString.call(new RegExp());// [object RegExp]
    Object.prototype.toString.call(new Error());// [object Error]
Copy the code

Triple kill

Interviewer: Good answer, let me give you some questions to do. It’s all datatype related, so let’s try it out

console.log(true + 0);
console.log(true + 'abc');
console.log(true+true);
console.log(true+false);
console.log(null + 1);
console.log(undefined + 1);
console.log(undefined + '123');
console.log('1'>true);
console.log('3'>true);
console.log(1+'2'+false);
console.log('2' + ['arr'.1]);
console.log(NaN= =NaN);
console.log(null= =undefined);
console.log(null= = =undefined);
console.log(1= =true);
console.log(0= = =false);
var p1 = {
    name: 'lisi'.age: 20
}

var p2 = {
    toString(){
        return 'name:wangwu,age:18'}}console.log('xi xi' + p1);
console.log('ha ha' + p2);
Copy the code

Me: 👴 don’t do it