Questions tagged [instanceof]

instanceof is an operator available in some object-oriented languages, including Java, php and JavaScript. Generally speaking, it allows the programmer to check whether an object passed as its left operand is an instance of a class specified by the right operand.

instanceof is an operator available in some object-oriented languages, including Java, php and JavaScript. It allows the programmer to check whether an object passed as its left operand is an instance of a class specified by the right operand.

instanceof in Java

instanceof in php

instanceof in JavaScript

958 questions
1918
votes
12 answers

Getting the class name of an instance

How do I find out the name of the class used to create an instance of an object in Python? I'm not sure if I should use the inspect module or parse the __class__ attribute.
Dan
  • 33,953
  • 24
  • 61
  • 87
517
votes
26 answers

What is the difference between typeof and instanceof and when should one be used vs. the other?

In my particular case: callback instanceof Function or typeof callback == "function" does it even matter, what's the difference? Additional Resource: JavaScript-Garden typeof vs instanceof
farinspace
  • 8,422
  • 6
  • 33
  • 46
506
votes
15 answers

What is the difference between instanceof and Class.isAssignableFrom(...)?

Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only difference that I know of is, when 'a' is null, the first returns false, while the second throws an exception. Other than that, do they always give…
Megamug
  • 6,277
  • 5
  • 21
  • 13
356
votes
29 answers

Is it possible to use the instanceof operator in a switch statement?

I have a question of using switch case for instanceof object: For example: my problem can be reproduced in Java: if(this instanceof A) doA(); else if(this instanceof B) doB(); else if(this instanceof C) doC(): How would it be…
olidev
  • 20,058
  • 51
  • 133
  • 197
353
votes
11 answers

What is the instanceof operator in JavaScript?

The instanceof keyword in JavaScript can be quite confusing when it is first encountered, as people tend to think that JavaScript is not an object-oriented programming language. What is it? What problems does it solve? When is it appropriate and…
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
352
votes
24 answers

The performance impact of using instanceof in Java

I am working on an application and one design approach involves extremely heavy use of the instanceof operator. While I know that OO design generally tries to avoid using instanceof, that is a different story and this question is purely related to…
Josh
  • 17,834
  • 7
  • 50
  • 68
332
votes
10 answers

Why does instanceof return false for some literals?

"foo" instanceof String //=> false "foo" instanceof Object //=> false true instanceof Boolean //=> false true instanceof Object //=> false false instanceof Boolean //=> false false instanceof Object //=> false 12.21 instanceof Number //=>…
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
313
votes
4 answers

Use of "instanceof" in Java

What is the 'instanceof' operator used for? I learned that Java has the instanceof operator. Can you elaborate where it is used and what are its advantages?
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
262
votes
3 answers

Javascript !instanceof If Statement

This is a really basic question really just to satisfy my curiosity, but is there a way to do something like this: if(obj !instanceof Array) { //The object is not an instance of Array } else { //The object is an instance of Array } The key…
ryandlf
  • 27,155
  • 37
  • 106
  • 162
232
votes
5 answers

C++ equivalent of java's instanceof

What is the preferred method to achieve the C++ equivalent of java's instanceof?
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
230
votes
6 answers

Why does 'instanceof' in TypeScript give me the error "'Foo' only refers to a type, but is being used as a value here."?

I wrote this code interface Foo { abcdef: number; } let x: Foo | string; if (x instanceof Foo) { // ... } But TypeScript gave me this error: 'Foo' only refers to a type, but is being used as a value here. Why is this happening? I thought…
Daniel Rosenwasser
  • 21,855
  • 13
  • 48
  • 61
214
votes
10 answers

How to perform runtime type checking in Dart?

Dart specification states: Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages). Sounds great, but…
Volo
  • 28,673
  • 12
  • 97
  • 125
196
votes
11 answers

Any reason to prefer getClass() over instanceof when generating .equals()?

I'm using Eclipse to generate .equals() and .hashCode(), and there is an option labeled "Use 'instanceof' to compare types". The default is for this option to be unchecked and use .getClass() to compare types. Is there any reason I should prefer…
Kip
  • 107,154
  • 87
  • 232
  • 265
187
votes
9 answers

typeof for RegExp

Is there anyway to detect if a JavaScript object is a regex? For example, I would like to do something like this: var t = /^foo(bar)?$/i; alert(typeof t); //I want this to return "regexp" Is this possible? Thanks! EDIT: Thanks for all the answers.…
tau
  • 6,499
  • 10
  • 37
  • 60
172
votes
18 answers

What is the 'instanceof' operator used for in Java?

What is the instanceof operator used for? I've seen stuff like if (source instanceof Button) { //... } else { //... } But none of it made sense to me. I've done my research, but came up only with examples without any explanations.
Ben
  • 1,745
  • 2
  • 11
  • 3
1
2 3
63 64