Today I was asked about JS data judgment in the interview. It’s a pity that I didn’t answer it. Here I make a summary.
The seven basic data types of JavaScript:
Basic data types: Undefined, Null, Symbol, Number, String, Boolean reference types: Object the difference is that one is the value of the variable is stored in the memory, the other one is stored in the memory address values of variables.
typeof
Give various typeof solutions for various types: typeof undefined — undefined typeof null — object typeof number — number typeof string — string typeof symbol — symbol typeof boolean — boolean typeof object — object typeof array — object typeof function — fn(){} If this is an arrow function, return ()=>{} unfortunately, null,object,array all return object
instanceof
Using this method, we can solve the problem of null, array,object. Null instanceof Object — false Object instanceof Object — true array instanceof Object — true Array instanceof array — true object instanceof array — false For example, for a complex variable type like Date, its instanceof is also an Object, but it is a special Object and still cannot be detected.
constructor
The variable is constructed from what its constructor is. Look at the code:
const date = new Date();
const arr = []
const obj = {}
console.log(date.constructor === Date); // true
console.log(arr.constructor === Date); // false
console.log(obj.constructor === Date); // false
Copy the code
Now, for complicated variables, we’ve solved the problem. Note, however, that this method is not very effective for determining variables from inherited classes
class A {
a(){
console.log("a")
}
}
class B extends A{
b(){
console.log("b")
}
}
const b = new B()
console.log(b.constructor === B); // true
console.log(b.constructor === A); // false
Copy the code
As you can see, I expect to get two true, but I get an error when I judge its inherited class. However, the Instanceof method does not report an error. So a lot of times, these two methods have to be used in conjunction with each other
Object.prototype.call
If we want to solve data type determination completely, we use Object.prototype.call. Call can define the value of this, and we pass in this to the current data. This returns the desired result, giving the code:
console.log(Object.prototype.toString.call(1)); --- [object Number]
console.log(Object.prototype.toString.call("1")); --- [object String]
console.log(Object.prototype.toString.call(true)); --- [object Boolean]
console.log(Object.prototype.toString.call(null)); --- [object Null]
console.log(Object.prototype.toString.call(undefined)); --- [object Undefined]
console.log(Object.prototype.toString.call(function a() {})); --- [object Function]
console.log(Object.prototype.toString.call(() => {})); --- [object Function]
console.log(Object.prototype.toString.call([])); --- [object Array]
console.log(Object.prototype.toString.call({})); --- [object Object]
Copy the code
Now you’re done. Why is object.toString.call () needed instead of toString? Because the number, the string toString, gives us the value of the current character instead of what we want, so we use the method on Object, and we get the result very quickly. Here I’m trying to encapsulate this method to quickly determine which type it is.
function _isTypeof(value){ const type = Object.prototype.toString.call(value); return type.match(/\[object (.*?)] /)[1].toLowerCase(); }Copy the code
This method has worked fine so far.
isArray,isNaN
IsArray is an array method that checks whether an object is an array. IsNaN is a method of determining number, whether an object is infinite or infinitesimal