VK said:
Truly I don't understand what are you currently doing neither.
Ah ok, that would explain it.
But I understand what you are trying to say now.
Instead of using instanceof and checking the "type" of an object, I
should use property checking.
And that is basically what I do now. I check if the object has all the
properties defined in the interface.
In Java
interfaces are used to work around the model limitation preventing
multiple inheritance. These are abstract classes one has to attach to
and redefine in the object instance. Respectively instanceof operator
had been patched there so say on
myClass extends someClass implements Runnable, MouseListener {...}
later (myClassInstance instanceof Runnable) is true which is
semantically and technically plain b.s. - myClassInstance instanceof
myClass and Object but no way of Runnable: yet this convenience patch
will stay in use.
I would be very annoyed if Java or any language would force me to use
different operators to test if it extends/implements a class or an
interface.
myClassInstance instanceof Runnable
makes perfect sense to me.
Javascript doesn't have abstract constructors
Actually I do. I throw an AbstractException in constructors for things I
regard as interfaces.
not it needs interfaces
for multiple inheritance. All you may care about is:
1) does obj have this particular object in its chain?
2) does this particular object has needed property/method
3) (sometimes occasionally) is this particular object located at the
specific point of the chain?
It may not be needed in the same sense that inheritance is not needed,
but I also desire a test if an object has all properties as defined in
another prototype. It may not be full-proof but it suffices.
A hypothetical example to demonstrate this:
function IDragListener()
{
}
IDragListener.prototype.dragDropEnd = function(event) {};
IDragListener.prototype.dragEnter = function(event) {};
IDragListener.prototype.dragExit = function(event) {};
IDragListener.prototype.dragOver = function(event) {};
function SpecialComponent()
{
var list = new ArrayList();
this.addListener = function(listener)
{
validate(arguments, [IDragListener]);
list.add(listener);
}
function fireDragEnterEvent()
{
var it = list.iterator();
while (it.hasNext())
{
var listener = it.next();
listener.dragEnterEvent(new DragEvent());
}
}
}
So I could make a test in each fire method to test if the added listener
has that particular property.
However I prefer to test it during adding the listener. I reality this
is actually only done in a debug mode. Doing it like this enables me to
find the error in an earlier stage and takes less code.