preface
Detailed about the types of judgment article is mainly about how to use typeof judgment basic data types and how to use the Object. The prototype. The toString judgment reference data types, and then on the basis of the two encapsulate a basic data types can be used to judge the type function can also be used to reference data type.
In this article, we will explain the usage of typeof and instanceof, and the principle of typeof and instanceof.
instanceof
Instanceof usage
The main purpose of instanceof is to determine whether an instance belongs to a certain type.
let Person= function () {
}
let nicole = new person()
personinstanceof Person // true
Copy the code
Of course, 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 person = new programmer()
person instanceof person // true
person instanceof programmer // true
Copy the code
This is instanceof. It’s not very complicated, but we need to understand the principle behind it as well as how it works.
Instanceof principle
In short, instanceof returns true only if the prototype of the right variable is on the prototype chain of the left variable, and false otherwise.
Here are some thought questions
function Foo() {
}
Object instanceof Object // true
Function instanceof Function // true
Function instanceof Object // true
Foo instanceof Foo // false
Foo instanceof Object // true
Foo instanceof Function // true
Copy the code
In fact, if you want to deeply understand the principle of Instanceof, you must understand the prototype. If you need it, you can see the breakdown prototype I wrote before.
Here’s a picture to illustrate the problem above
So just remember before you solve this problem,instanceof works by saying that the prototype of the right variable is on the prototype chain of the left variable, so just remember that and combine that with the picture above, and you’ll see everything.
Object instanceof Object
What is Object? Constructors, to put it bluntly, are functions, what was the last prototype of all objects? Function. Prototype. __proto__ = function.prototype.__proto__ = function.prototype.__proto__ = Function. Object. Prototype. All final results are of course true.
Function instanceof Function and Function instanceof Object can be analyzed in accordance with the above method, and the final results are consistent.
Foo instanceof Foo
Why did I put this back false? Foo’s prototype is Function. Prototype. Foo. So of course it will return false.
Hand write an instanceof
Function newInstanceOf(leftValue, rightValue) {return false if(typeof left! == 'object' || left === null) return false; let rightProto = rightValue.prototype; While (true) {if (leftValue === null) {return false; If (leftValue === rightProto) {return true; // If (leftValue === rightProto) {return true; } leftValue = object.getProtoTypeof (leftValue);} leftValue = object.getProtoTypeof (leftValue); If not, continue the loop}}Copy the code
Typeof principle
Different objects are represented as binary at the bottom, and the first three bits of the binary store their type information in JS.
000: object 010: floating-point number 100: string 110: Boolean 1: integer Typeof null is equal to object because the first (lower) bits of null are zeros, so “object” is returned when typeof is executed.
Note: the front here is counted from right to left.
The reason typeof can’t distinguish between data types such as Array and Date is because the first three digits in the underlying binary representation are zeros. (In the original version of JS, the 32-bit system was used and the type information of the variable was stored low for performance reasons.)
conclusion
- Base datatypes are defined as typeof. Note that null is an exception
- Reference data type with the Object. Peototype. ToString method, using instanceof also not line, of course, but with bad prone to errors, such as an array, it can be
instanceof
Judgment for the Object. - The best method is to encapsulate a function type judgment, basic data types with typeof judgment, reference data type with the Object. The prototype. ToString judgment.
- Instanceof is primarily used to determine the dependency of an instance to a constructor