Java instanceof
Instanceof is strictly a binocular operator in Java that tests whether an object is an instanceof a class
boolean result = obj instanceof Class
Where obj is an object, Class is a Class or interface, result is true when obj is an object of Class, a direct or indirect subclass of obj, or an implementation of its interface, otherwise false.
The summary is:
-
Returns true as long as the object obj is judged to be on the inheritance chain belonging to Class
-
Obj must be an object, so obj must be a reference type, not a primitive type
-
Byte Short int long float Double char Boolean
-
Special type: NULL
This type has no name, so it is not possible to declare a variable of type NULL or convert it to type NULL. A null reference is the only possible value for a null expression. A null reference can also be converted to any reference type
-
If obj is null, false is returned.
The compiler checks to see if obj can be converted to the class type on the right. If it cannot be converted, an error is reported. If the type cannot be determined, it is compiled at runtime.
Java data types see Java Data Structures again – Analyzing The Underlying Implementation and Application Considerations
Object Array Map Set object Array Map Set
For the front end, this is just an introduction
JavaScript instanceof
The instanceof operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object.
The instanceof operator is used to test whether constructor. Prototype exists on the prototype chain of the parameter Object.
The internal implementation mechanism of the Instanceof operator is directly related to implicit and explicit archetypes. An lvalue of instanceof is typically an object, and an rvalue is typically a constructor that determines whether an lvalue is an instanceof an rvalue. It is implemented by tracing the __proto__ of an lvalue all the way to the end of the prototype chain until it equals the prototype of an rvalue.
Instanceof is used to determine whether an object is an instanceof a function. For example, obj instanceof FN actually checks whether fn’s prototype is on the prototype chain of OBJ. so
The essence of the instanceof operator: tests whether constructor. Prototype exists on the prototype chain of the parameter Object.
Native JS implements instanceof functionality
The core is whether the __proto__ on the left object points to the prototype property on the right
function instanceofMethod (left, right) { let prototype = right.prototype; let proto = left.__proto__; while (true) { if (proto === prototype) return true; if (proto === null) return false; Proto = proto.__proto__; proto = proto.__proto__; }}Copy the code
Javascriptjs Archetypes, Prototype Chains, and Inheritance
We can use the instanceof operator to look at the relationship between Object and Function:
console.log(Object instanceof Object); // true
console.log(Object instanceof Function); // true
console.log(Function instanceof Object); // true
console.log(Function instanceof Function); // true
Copy the code
Function and object depend on each other, respectively defined the description method of things and things of the generation method, in the process of generating JS everything is indispensable.
Function instanceof Function // true, why? The prototype is a Function objectCopy the code
-
Prototype comes out of thin air. Syntactically, all {} are interpreted as new Object();
-
Function special in __proto__ == prototype. Syntactically, all Function declarations are interpreted as new Function().
Let’s see what makes Function and Object special:
-
Object is created by Function: because object.__proto__ === funciton. prototype;
-
Function. Prototype is created by Object. Prototype;
-
Funciton was created by Function itself!
-
Object. Prototype came out of thin air!
null instanceof null
Basic wrapper type objects: ECMAScript also provides three special reference types: Boolean, Number, and String. These types are similar to other built-in object types, but have special behavior corresponding to their base types. In fact, every time a primitive type is read, an object of the corresponding primitive wrapper type is created behind the scenes, allowing us to call methods to manipulate the data. Wrapper type is an API object that specifically encapsulates a value of a primitive type and provides operations on the value of the primitive type
What are the differences between other built-in objects and primitive wrapper type objects?
Ordinary built-in objects and basic type of packaging the main difference is the lifetime of objects, use the new operator to create instances of a reference type, before the execution flow away from the current scope are kept in memory, and automatically create basic packing type of object, the only exists in one line of code execution in an instant, then immediately be destroyed immediately. This means that we can no longer add properties and methods to primitive wrapper type values at runtime, and there is no such thing as constructor.
so
console.log('a' instanceof String)//false
console.log(0 instanceof Number)//false
console.log(false instanceof Boolean)//false
Copy the code
Null is the ancestor of all objects
console.log(null instanceof null)
^
TypeError: Right-hand side of ‘instanceof’ is not an object
In fact, compared with Java, much the same
Reprint the home station article “the essence of the instanceof operator: Java inheritance chain and JavaScript prototype chain”, please indicate the source: www.zhoulujun.cn/html/webfro…