I'd like to check if the object x implements (is instance of) the mixin MyInterface:
Ext.define('MyInterface', {
interfaceMethod: Ext.emptyFn
});
Ext.define('Foo', {
mixins: {
myInterface: 'MyInterface'
}
});
var x = new Foo();
console.log(x instanceof MyInterface);
var isInstanceOfMyInterface = false;
for (var i in x.mixins) {
if (x.mixins[i].$className == 'MyInterface') {
isInstanceOfMyInterface = true;
}
}
console.log(isInstanceOfMyInterface);
The workaround has one major issue: it doesn't work if a subclass of MyInterface is used.
jsfiddle
A pattern the Ext core uses is to apply a mixin specific property so you can test for its existence. It's a lot cheaper than an instanceof check as well. For example:
Ext.define('MyInterface', {
isMyInterface: true,
interfaceMethod: Ext.emptyFn,
});
Ext.define('Foo', {
mixins: {
myInterface: 'MyInterface'
}
});
var x = new Foo();
console.log(x.isMyInterface);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With