문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top