


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.

instanceof的作用是判断一个对象是否是某个类的实例,或者是否实现了某个接口。instanceof是一个用于检查对象是否是指定类型的运算符。instanceof运算符使用场景:1、类型检查:可以用来判断一个对象的具体类型,以便根据不同类型执行不同的逻辑;2、接口判断:可以用来判断一个对象是否实现了某个接口,以便根据接口的定义调用相应的方法;3、向下转型等等。

在Java中,instanceof是一个二元运算符,用于检查一个对象是否是一个类的实例,或者是一个类的子类的实例,其语法形式为“object instanceof class”,其中,object是一个对象引用,class是一个类名或者接口名。

概念1、该运算符用于操作对象的例子,检查对象是否为特定类型(类型或接口类型)。格式2、如果计算器左侧变量所指的对象是操作器右侧类或接口的对象,则结果是真实的。(Objectreferencevariable)instanceof(class/interfacetype)实例packagecom.verify_instanceof;publicclassTestInstanceOf{publicstaticvoidmain(String[]args){//下面四行代码用来证明:instanceof

该运算符仅用于对象引用变量。该运算符检查对象是否属于特定类型(类类型或接口类型)。instanceof运算符写为-(Objectreferencevariable)instanceof(class/interfacetype)如果运算符左侧变量引用的对象通过了右侧类/接口类型的IS-A检查,则结果将为true。以下是一个示例-示例 现场演示publicclassTest{ publicstaticvoidmain(Stringargs[]){&nbs

在JavaScript中,typeof是一个用来确定给定变量的数据类型的操作符。可以用来确定一个变量是字符串、数字、布尔值、函数、对象或undefined的数据类型。

在使用instanceof运算符检查对象的类型时,如果结果为true,表示对象是指定类型的实例。但是,编译器并不会自动将对象转换为指定类型,因此需要进行强制类型转换。强制类型转换是将一个对象从一种类型转换为另一种类型的操作。在使用instanceof运算符后,如果确定对象是指定类型的实例,并且希望以该类型进行操作,就需要进行强制类型转换。

instanceof不可以接口。原因是:instanceof关键字用于检查一个对象是否是特定类的实例,不能直接用于检查一个对象是否是接口的实例,因为接口不能被实例化。

不用instanceof的原因有:1、正在使用的编程语言可能不支持 instanceof运算符,;2、认为使用其他方法可以更好地实现需求,在某些情况下,使用其他方法来检查对象类型可能更有效或更适合你的需求;3、不熟悉 instanceof运算符的使用方式或不确定它的行为;4、在某些情况下,使用 "instanceof" 可能不是最佳的选择。


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
