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
69
votes
15 answers

How to efficiently check if variable is Array or Object (in NodeJS & V8)?

Is there any way to efficiently check if the variable is Object or Array, in NodeJS & V8? I'm writing a Model for MongoDB and NodeJS, and to traverse the object tree I need to know if the object is simple (Number, String, ...) or composite (Hash,…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
68
votes
2 answers

instanceof negation

Which is the correct format for negating instanceof? if ( ! $a instanceof stdClass) or if ( ! ($a instanceof stdClass) ) I've convinced myself the latter is correct way, probably after reading a blog article several years ago, but after some…
Mathew
  • 8,203
  • 6
  • 37
  • 59
65
votes
8 answers

How instanceof will work on an interface

instanceof can be used to test if an object is a direct or descended instance of a given class. instanceof can also be used with interfaces even though interfaces can't be instantiated like classes. Can anyone explain how instanceof works?
developer
  • 9,116
  • 29
  • 91
  • 150
64
votes
8 answers

Avoiding 'instanceof' in Java

I have the following (maybe common) problem and it absolutely puzzles me at the moment: There are a couple of generated event objects which extends the abstract class Event and I want to divide them to Session Beans, like public void…
fnst
  • 5,574
  • 4
  • 23
  • 18
63
votes
3 answers

How to "instanceof" a primitive string (string literal) in JavaScript

In JavaScript, I can declare a string in the following ways; var a = "Hello World"; var b = new String("Hello World"); but a is not an instance of String... console.log(a instanceof String); //false; console.log(b instanceof String); //true; So…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
62
votes
4 answers

Does instanceof return true if instance of a parent?

I have a class Child that extends Parent. Parent child = new Child(); if (child instanceof Parent){ // Do something } Does this return true or false, and why?
ikbal
  • 1,844
  • 4
  • 26
  • 46
60
votes
5 answers

How to determine if an object is an instance of certain derived C++ class from a pointer to a base class in GDB?

I'm debugging a C++ program with GDB. I have a pointer to an object of certain class. The pointer is declared to be of some super class which is extended by several sub-classes. There is no fields in the object to specify the precise class type of…
user1101096
  • 699
  • 2
  • 6
  • 7
59
votes
3 answers

instanceof - incompatible conditional operand types

The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this doesn't: String s = new String(); System.out.println(s instanceof Cloneable); A compiler error is thrown. What is the problem?
java_geek
  • 17,585
  • 30
  • 91
  • 113
58
votes
7 answers

Check if an object is an instance of a class (but not an instance of its subclass)

For this example: public class Foo{} public class Bar extends Foo{} .... void myMethod(Foo qux){ if (checkInstance(qux,Foo.class)){ .... } } How can I check if qux is an instance of Foo (but not an instance of its subclass of foo)?…
Addev
  • 31,819
  • 51
  • 183
  • 302
56
votes
3 answers

Switch by class (instanceof) in PHP

It is possible to replace block of if( .. instanceof ...), elseif(... instanceof ...), ... with switch? For example:
leninzprahy
  • 4,583
  • 3
  • 17
  • 13
53
votes
4 answers

PHP check for instance of DateTime?

Is this the only way to check if an object is an instance of a class, in my case of the DateTime class? $cls = ReflectionClass("DateTime"); if (! $cls->isInstance( (object) $var ) ) { // is not an instance } It seems a bit heavy to me.
Niklas R
  • 16,299
  • 28
  • 108
  • 203
51
votes
3 answers

What's the difference between isPrototypeOf and instanceof in Javascript?

In some of my own older code, I use the following: Object.prototype.instanceOf = function( iface ) { return iface.prototype.isPrototypeOf( this ); }; Then I do (for example) [].instanceOf( Array ) This works, but it seems the following would do…
Steffen Heil
  • 4,286
  • 3
  • 32
  • 35
51
votes
3 answers

Java - null instanceof Object evaluates to both true and false

When I compile and run this code: public class Testing { public static void main(String... args) { Object obj = null; if (obj instanceof Object) { System.out.println("returned true"); } else { …
Infiniteh
  • 634
  • 5
  • 8
45
votes
9 answers

What's the point of new String("x") in JavaScript?

What are the use cases for doing new String("already a string")? What's the whole point of it?
Pacerier
  • 86,231
  • 106
  • 366
  • 634
45
votes
7 answers

How to avoid 'instanceof' when implementing factory design pattern?

I am attempting to implement my first Factory Design Pattern, and I'm not sure how to avoid using instanceof when adding the factory-made objects to lists. This is what I'm trying to do: for (Blueprint bp : blueprints) { Vehicle v =…
Charles H
  • 625
  • 1
  • 7
  • 11
1 2
3
63 64