Because Javascript is a weak language,
The parameter cannot specify the type,
Therefore, a method isinstence (OBJ) is provided to determine whether the object is of the type that implements this interface.
Note that this interface will not be added to the original function chain after implementation.
(This function supports multiple interfaces at the same time)
<Script language = "JavaScript">
Function. Prototype. exchain = new array ();
Function. Prototype. implements = function (ITF)
{
VaR innerobj = new this ();
For (I in ITF. Abstruct)
{
If (ITF. Abstruct [I]. constructor = function)
{
If (! Innerobj [I])
{
Throw new error (9001, "implement all Abstract Functions in the interface. ")
}
}
}
This. exchain. Push (ITF. constructor );
This. exchain. Push (this );
Innerobj = NULL;
Return this;
}
VaR Abstruct = function (){}
Function. Prototype. isinstence = function (OBJ)
{
For (VAR I = 0; I <obj. constructor. exchain. length; I ++)
{
If (this = obj. constructor. exchain [I])
{
Return true;
}
}
If (this = obj. constructor)
{
Return true;
}
Return false;
}
//////////////////////////////////////// /////////////
Interfacea = function ()
{
This. Abstruct = new Abstruct ();
This. Abstruct. Show = function (){}
// This. Abstruct. getmsg = function (){}
}
Interfaceb = function ()
{
This. Abstruct = new Abstruct ();
This. Abstruct. showb = new function ();
}
VaR classa = function ()
{
This. Show = function ()
{
Alert ("classa ");
}
This. showb = function ()
{
Alert ("classa. showb ")
}
}. Implements (New interfacea (). implements (New interfaceb ())
VaR O1 = new classa ();
O1.show ();
O1.showb ();
Alert (interfacea. isinstence (O1 ));
Alert (interfaceb. isinstence (O1 ));
Alert (classa. isinstence (O1 ));
VaR classb = function ()
{
}
Alert (classb. isinstence (O1 ));
</SCRIPT>