Question

I'm using dojo / AMD to create a new object and am using instanceof to test it.

Currently the code is this:

var object = new (declare([_BaseField, ValidationTextBox]))({
      params: someParams
});


console.log(object instanceof _BaseField); // Returns true.
console.log(object instanceof ValidationTextBox); // Returns false.

My question is, show can I test this if instanceof doesn't recognize multiple inheritance?

Was it helpful?

Solution

JavaScript only has single inheritance, and therefore a linear prototype chain. a instanceof B tests if B is in the prototype chain of a.

Emulating multiple inheritance, dojo takes the first object as the prototype, and treats all remaining objects as mixins.

Therefore the objects _BaseField and ValidationTextBox are not used equally.

To test that object also inherits from ValidationTextBox the dojo way, you have to test the dojo way, i.e.

object.isInstanceOf(ValidationTextBox) // true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top