Delan Azabani
Delan Azabani

Reputation: 81404

testing if constructor in constructor chain

I'm attempting to implement a GTK+ inspired widget toolkit for the web in JavaScript. One of the constructor chains goes

gtk.widget => gtk.container => gtk.bin => gtk.window

Every gtk.widget has a showAll method, which, if and only if the widget is a gtk.container or derivative (such as gtk.bin or gtk.window), will recursively show the children of that widget. Obviously, if it isn't a gtk.container or derivative, we shouldn't do anything because the widget in question can't contain anything.

For reference, here is my inheritance function; it's probably not the best, but it's a start:

function inherit(target, parent) {
    target.prototype = new parent;
    target.prototype.constructor = target;
}

I know that I can check for the direct constructor like this:

if (this.constructor === gtk.container) ...

However, this only tests for direct construction and not, say, if the object is a gtk.bin or gtk.window. How can I test whether gtk.container is somewhere up in the constructor chain?

Upvotes: 0

Views: 172

Answers (1)

Andy E
Andy E

Reputation: 344605

Use the instanceof operator:

if (this instanceof gtk.container) ...

Upvotes: 2

Related Questions