2

I want to test if a variable is an instance of the current class. So I'm checking within a method of the class. And I was hoping there's a more abstract way of doing than specifying the class name. In PHP it's possible with the self keyword.

In PHP it's done like this:

if ($obj instanceof self) {

}

What's the equivalent in nodejs ?

zero298
  • 25,467
  • 10
  • 75
  • 100
Alex
  • 66,732
  • 177
  • 439
  • 641
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof –  Oct 01 '18 at 20:07
  • How doesn't Google answer this – Greggz Oct 01 '18 at 20:09
  • Possible duplicate of [How to test same object instance in Javascript?](https://stackoverflow.com/questions/26214966/how-to-test-same-object-instance-in-javascript) – Ruzihm Oct 01 '18 at 20:09
  • you dont understand. I want to test if a variable is an instance of the current class. So I'm checking within a method of the class. And I was hoping there's a more abstract way of doing than specifying the class name. In PHP it's possible with the self keyword – Alex Oct 01 '18 at 20:12
  • @Ruzihm that's not the same question. The one you propose is about the same *instance*, as in if two variables are literally pointing to the same object in memory. The `instanceof` operator tells you if it's the same *type* of object. E.g, `new String("a") == new String("a")` would return `false` but `instanceof String` would be `true`. – VLAZ Oct 01 '18 at 20:13
  • 1
    Perhaps you're looking for `thing instanceof this.constructor`. – CRice Oct 01 '18 at 20:14
  • 3
    You didn't specify the context for this code. But in case it's executed in instance method, then this is as previous comment says. This is not specific to instanceof. Current class can be referred as `this` in static methods and as `this.constructor` in instance methods. – Estus Flask Oct 01 '18 at 20:21
  • Please add that to your question then. You don't have PHP tagged and the people that are looking at your question are going to be confused on what you are actually trying to do. Please spell out, as you have done in the comments, what you are trying to do so that it is no longer unclear what you are asking. – zero298 Oct 01 '18 at 20:25

1 Answers1

2

Considering your comment (emphasis mine):

I want to test if a variable is an instance of the current class. So I'm checking within a method of the class. And I was hoping there's a more abstract way of doing than specifying the class name. In PHP it's possible with the self keyword.

I would say that self in this instance would kind of map to this.constructor. Consider the following:

class Foo {}
class Bar {}
class Fizz {
  // Member function that checks if other 
  // is an instance of the Fizz class without 
  // referring to the actual classname "Fizz"
  some(other) {
    return other instanceof this.constructor;
  }
}

const a = new Foo();
const b = new Foo();
const c = new Bar();
const d = new Fizz();
const e = new Fizz();

console.log(a instanceof b.constructor); // true
console.log(a instanceof c.constructor); // false
console.log(d.some(a)); // false
console.log(d.some(e)); // true
zero298
  • 25,467
  • 10
  • 75
  • 100