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
43
votes
4 answers

Java: instanceof Generic

Isn't there any way to find the class-type of a generic? if (T instanceof String) { // do something... } The above definitely does not compile.
someguy
  • 7,144
  • 12
  • 43
  • 57
43
votes
9 answers

Switch over type in java

Before I start, I know there are a bunch of answers to this question that suggest alternate approaches. I'm looking for assistance to this particular approach as to whether it is possible, and if not, similar approaches that might work. I have a…
ewok
  • 20,148
  • 51
  • 149
  • 254
39
votes
7 answers

instanceof check in EL expression language

Is there a way to perform an instanceof check in EL? E.g. #{errorMessage1} #{errorMessage2}
Francesco
  • 2,350
  • 11
  • 36
  • 59
37
votes
3 answers

JavaScript inheritance and the constructor property

Consider the following code. function a() {} function b() {} function c() {} b.prototype = new a(); c.prototype = new b(); console.log((new a()).constructor); //a() console.log((new b()).constructor); //a() console.log((new c()).constructor);…
Quolonel Questions
  • 6,603
  • 2
  • 32
  • 33
37
votes
12 answers

Why cast after an instanceOf?

In the example below (from my coursepack), we want to give to the Square instance c1 the reference of some other object p1, but only if those 2 are of compatible types. if (p1 instanceof Square) {c1 = (Square) p1;} What I don't understand here is…
JDelage
  • 13,036
  • 23
  • 78
  • 112
34
votes
5 answers

PHP instanceof for traits

What is the proper way to check if a class uses a certain trait?
xpedobearx
  • 707
  • 3
  • 8
  • 13
32
votes
6 answers

Is This Use of the "instanceof" Operator Considered Bad Design?

In one of my projects, I have two "data transfer objects" RecordType1 and RecordType2 that inherit from an abstract class of RecordType. I want both RecordType objects to be processed by the same RecordProcessor class within a "process" method. My…
30
votes
2 answers

Java instanceof with changing objects

I need a method where i could pass on a parameter which i assume would be a Class (not sure though) and in that method, instanceof would be used to check if x is an instance of the passed Class. How should i do that? I tried a few things but none…
Nicolas Martel
  • 1,641
  • 3
  • 19
  • 34
29
votes
2 answers

instanceof check on interface

Given the following code: module MyModule { export interface IMyInterface {} export interface IMyInterfaceA extends IMyInterface {} export interface IMyInterfaceB extends IMyInterface {} function(my: IMyInterface): void { if (my…
Bart van den Burg
  • 2,166
  • 4
  • 25
  • 42
29
votes
6 answers

Java - is there a "subclassof" like instanceof?

Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance!
rasgo
  • 1,381
  • 4
  • 15
  • 28
28
votes
1 answer

extracting data from typing types

I am having some issues working with the typing types in Python for any more than type hinting: >>> from typing import List >>> string_list = ['nobody', 'expects', 'the', 'spanish', 'inqusition'] >>> string_list_class = List[str] Now I would like…
Hans
  • 2,354
  • 3
  • 25
  • 35
28
votes
4 answers

Throw and catch an exception, or use instanceof?

I have an exception in a variable (not thrown). What's the best option? Exception exception = someObj.getExcp(); try { throw exception; } catch (ExceptionExample1 e) { e.getSomeCustomViolations(); } catch (ExceptionExample2 e) { …
Ruslan
  • 14,229
  • 8
  • 49
  • 67
28
votes
2 answers

Differences between typeof and instanceof in JavaScript

I'm working with node.js, so this could be specific to V8. I've always noticed some weirdness with differences between typeof and instanceof, but here is one that really bugs me: var foo = 'foo'; console.log(typeof foo); Output:…
Eric
  • 999
  • 2
  • 8
  • 23
26
votes
5 answers

See if two object have the same type

Let's say that I have a class A, and that B,C,D are derived from A. If I want to know what's the type of an object referenced, I can declare: // pseudo-code if(obj instanceof B) < is B> else if(obj instanceof C) < is C> else
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
25
votes
2 answers

Instanceof equivalent for Object.create and prototype chains

Consider such an object with a prototype chain: var A = {}; var B = Object.create(A); var C = Object.create(B); How to check in runtime if C has A in its prototype chain? instanceof doesn't fit as it's designed to work with constructor functions,…
Kos
  • 70,399
  • 25
  • 169
  • 233