


The difference between javascript instanceof and typeof_javascript skills
Why is the result false?
You have to distinguish the difference between string and String
aColors[0] is a string value type, of course not an instance of String La. Refer to the following code
var aColors = ["red", "green", "blue"];
aColors[0]= new String("1")
alert(typeof aColors[0]); //output "Object"
alert(aColors[0] instanceof String); //output "true";
For more information, please refer to the following article:
instanceof Operators
return a Boolean value indicating whether the object is an instance of a specific class.
result = object instanceof class
Parameters
result
Required. Any variable.
object
Required. Any object expression.
class
Required. Any defined object class.
Description
The instanceof operator returns true if object is an instance of class. Returns false if object is not an instance of the specified class, or if object is null.
Example
The following example illustrates the usage of instanceof operator.
function objTest(obj){
var i, t, s = ""; // Create variable.
t = new Array(); // Create an array.
t["Date"] = Date; // Fill the array.
t["Object"] = Object;
t["Array"] = Array;
for (i in t)
{
if (obj instanceof t[i]) / / Check the class of obj.
{
s = "obj is an instance of " i "n";
}
else
{
s = "obj is not an instance of " i "n" ;
}
}
return(s); // Return a string.
}
var obj = new Date();
document.write(objTest(obj));
Instanceof and typeof can both be used to determine whether a variable is empty Or what type of variable it is.
Typeof is used to obtain the type of a variable. Typeof generally can only return the following results: number, boolean, string, function, object, undefined. We can use typeof to get whether a variable exists, such as if(typeof a!="undefined"){}, instead of using if(a) because if a does not exist (undeclared), an error will occur. For Array, Null When using typeof for special objects, object will always be returned. This is the limitation of typeof.
If we want to get whether an object is an array, or determine whether a variable is an instance of an object, we must use instanceof. instanceof is used to determine whether a variable is an instance of an object. For example, var a=new Array(); alert(a instanceof Array); will return true, and alert(a instanceof Object) will also return true; this is because Array is subclass of object. Another example: function test(){};var a=new test();alert(a instanceof test) will return true.
When it comes to instanceof, we have to insert one more problem, which is the arguments of function. We may all think that arguments are an Array, but if you use instanceof to test, you will find that arguments are not an Array object, even though it looks similar.
Also:
Test var a=new Array();if (a instanceof Object) alert('Y');else alert('N');
Get 'Y'
But if (window instanceof Object) alert('Y');else alert('N');
Get'N'
So, the object tested by instanceof here refers to the object in js syntax, not the dom model object.
There will be some differences when using typeof
alert(typeof(window) will get object
When you are young, you should talk less and do more.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The function of instanceof is to determine whether an object is an instance of a certain class, or whether it implements a certain interface. instanceof is an operator used to check whether an object is of a specified type. Usage scenarios of instanceof operator: 1. Type checking: can be used to determine the specific type of an object, so as to perform different logic according to different types; 2. Interface judgment: can be used to determine whether an object implements an interface, so as to determine whether an object implements an interface. The definition of the interface calls the corresponding method; 3. Downward transformation, etc.

Concept 1. This operator is used to operate on objects and check whether the object is of a specific type (type or interface type). Format 2. If the object pointed to by the variable on the left side of the calculator is an object of the class or interface on the right side of the operator, the result is true. (Objectreferencevariable)instanceof(class/interfacetype) instance packagecom.verify_instanceof;publicclassTestInstanceOf{publicstaticvoidmain(String[]args){//The following four lines of code are used to prove: instanceof

instanceof is an operator in JavaScript, used to detect whether the "prototype" attribute of the constructor appears anywhere in the prototype chain of the object. The syntax is "object instanceof constructor", where object is the object to be detected and constructor is the object to be detected. Constructor to check.

In Java, instanceof is a binary operator used to check whether an object is an instance of a class or an instance of a subclass of a class. Its syntax is "object instanceof class", where object is an object Quote, class is a class name or interface name.

The reasons for not using instanceof are: 1. The programming language you are using may not support the instanceof operator; 2. You think that using other methods can better achieve the requirements. In some cases, using other methods to check the object type may be more effective. or more suitable for your needs; 3. Not familiar with how the instanceof operator is used or unsure of its behavior; 4. In some cases, using "instanceof" may not be the best choice.

When using the instanceof operator to check the type of an object, if the result is true, it means that the object is an instance of the specified type. However, the compiler does not automatically convert the object to the specified type, so a cast is required. Casting is the operation of converting an object from one type to another. After using the instanceof operator, if you determine that the object is an instance of the specified type and want to operate with that type, you need to perform cast type conversion.

The reason is: The instanceof operator is used to check whether an object is an instance of a specific class (or its derived class). If the object is not an instance of a class, then type determination cannot be made and an error is thrown. To avoid this error, when using the instanceof operator, you need to ensure that the object is an instance of a class. If you are not sure about the type of an object, you can use other methods to determine the type.

This operator is only used for object reference variables. This operator checks whether an object belongs to a specific type (class type or interface type). The instanceof operator is written as -(Objectreferencevariable)instanceof(class/interfacetype) If the object referenced by the variable on the left side of the operator passes the IS-A check of the class/interface type on the right side, the result will be true. Here is an example - Example Live Demonstration publicclassTest{ publicstaticvoidmain(Stringargs[]){&nbs
