This is the fifth day of my participation in Gwen Challenge
preface
The interview series has been updated again.
1. Classification of data types
Primitive types: undefined, Boolean, number, string, bigInt, symbol, null
Object type: Object (Object, Array, Map, Set, WeakMap, WeakSet, Date……)
Primitive type, which stores a value. Object types, on the other hand, are derived from native Object and store a location (address/pointer).
// Object types can call methods
/ / the list = > [1, 2, 3, 4, 5] = > Array () = > Array (). The filter
const list = [1.2.3.4.5] // Object type
const filterItem = list.filter(i= > i === 2) / / [2]
// What happens if a method is called on a primitive type?
const number = 1 // Primitive type
number.toString() / / "1"
// 1. new Number(Number) => 1
ToString () 1 => Number() => Object() => Object().toString
// 3. Return the result, destroy the current instance.
Copy the code
2. Type judgment
Typeof: Only used to check the original type. If the object type is checked, only Object is returned.
Instanceof: operator used to check whether the constructor’s prototype property appears on the prototype chain of an instance object.
//typeof
console.log(typeof 42); // number
console.log(typeof 'aaaaa'); // string
console.log(typeof true); //boolean
console.log(typeof []); // What should I do if I want to print an array?
//instanceof
function a (){}
function b (){}
let c = new a()
console.log(c instanceof a) // true
console.log(c instanceof b) // false
Copy the code
Things to watch out for:
-
Undefined: creates a variable but is not assigned a value, which will have a default value of undefined.
-
Object is a special non-data structure type of any constructed Object instance, almost all data types created by new.
-
Instanceof principle and prototype chain
First, all objects have __proto__ attributes, only object.prototype. proto is null. The second point is that the prototype of all constructors refers to its prototype object, and the __proto__ of constructor instances also refers to the prototype object.
function a () {} const b = new a () console.log(b.__proto__ === a.prototype) // true refers to the prototype object of constructor A console.log(b instanceof a) // true console.log(b instanceof Object) // true // The nstanceof principle is to check whether a prototype appears on the prototype chain. Copy the code
sample
Is there a better way to determine type?
const checkType = obj= > {
const [typeEx, typeEn] = Object.prototype.toString.call(obj).split(' ')
return typeEn.substring(0, typeEn.length - 1).toLowerCase()
}
checkType(1) // number
checkType([]) // array
checkType(new Date()) // date
Copy the code
Can you implement Instanceof by hand?
const instanceOf = (left, right) = > {
const rpt = right.prototype
white(true){
if(left === null) return false
if(left === rpt) return true
left = left.__proto__
}
}
instanceOf([], Object) // true
instanceOf([], String) // false
instanceOf('a'.String) // true
Copy the code
The last
Interviewer: Do you know Callback Hell?
Interviewer: What’s the difference between react and Vue?
Interviewer: Do you know anything about ES6?
Interviewer: Do you know anything about Webpack?
Interviewer: Have you ever written a loader and separated modules while using WebPack?