I will go back to my hometown for the New Year tomorrow. I haven't finished talking about this "using inheritance in Javascript object-oriented programming" topic. If it is not completed, it will be delayed until next year. So I still have time to finish it. This year's work will be done, and there will be more important tasks next year !~~ Next, let's continue to look at the fourth Writing Method of the word "shen.
The fourth Writing Method of the word "NLP,Additional Inheritance LawAlthough it was invented by myself, and there are some shadows of the first three inheritance laws, this method is undeniable and can cut down all the issues mentioned above about inheritance. Next, let's take a closer look at why it is so martial arts and smart?
Principles of additional Inheritance Law:
Key to additional Inheritance LawCodeIs in its construction function arraylist04:
This . Base = New Collectionbase ();
For ( VaR Key In This . Base)
{
If ( ! This [Key])
{
This [Key] = This . Base [Key];
}
}
In fact, it is not important not to attach a base to this, and it will not affect our inheritance method at all. First, we can see that in the first sentence of the constructor, we immediately created a new base class instance. This shows that our inheritance has no requirements for writing the base class, use the frontInstance inheritance lawThe statement in is that all classes that the script engine thinks are correct. We knowBuild Inheritance LawWhy is there a problem? It is because it never creates a base class instance. WhilePrototype Inheritance MethodAlthough a base class instance is also created, it directly assigns the accumulated instance to the prototype attribute of the subclass, so that it has special requirements for writing the subclass.
Next, a for (in) loop will attach all the attributes and methods of the base class to the subclass instance this. This is also the reason why I call this inheritance method an additional method. This step andBuild Inheritance LawThe principle is quite similar, but the construction Inheritance Method is a technique that uses this scope replacement. The additional process is completed by the base class constructor, however, it also imposes special requirements on the construction of inheritance laws, and does not support prototype. Of course, the additional method still does not have this requirement.
Update of the additional Inheritance Law: Object. Prototype. extends = Function (Baseclass)
{
If (Arguments. Length > = 6 )
{
Throw New Error ('only can supprot at most 5 Parameters .');
}
VaR Base;
If (Arguments. Length > 1 )
{
VaR Arg01 = Arguments [ 1 ];
VaR Arg02 = Arguments [ 2 ];
VaR Arg03 = Arguments [ 3 ];
VaR Arg04 = Arguments [ 4 ];
Base = New Baseclass (arg01, arg02, arg03, arg04 );
}
Else
{
Base = New Baseclass ();
}
For ( VaR Key In Base)
{
If ( ! This [Key])
{
This [Key] = Base [Key];
If ( Typeof (Base [Key]) ! = ' Function ')
{
Delete Base [Key];
}
}
}
This . Base = Base;
// Base. inherit = this;
};
In this way, we can directly write the inheritance as follows:
Function Arraylist04 ()
{
This . Extends (collectionbase );
// ...
}
It also provides support for passing parameters to the base class when inheriting the base class, such:
Function Listitem ()
{
This . Extends (listitembase, text, value );
// ...
}
For the base class, new listitembase (text, value); is executed to generate a base class instance.
Defects of additional Inheritance Law:
From the perspective of my current use, if I do not use the override technology to override the method, and then call the method of the base class in the new method (I will talk about this technology later, because it does not affect and does not belong to the inheritance method we are discussing today. There are basically no defects in the appending method. To do this, you must use a for (in) loop to import the base class. The syntax is ugly :(
Example of additional Inheritance Law:
Document. Write ('additional Inheritance Method: < BR > ');
VaR Arraylist41 = New Arraylist04 ();
Arraylist41.add ('A ');
Arraylist41.add ('B ');
Arraylist41.foo ();
VaR Arraylist42 = New Arraylist04 ();
Arraylist42.add ('A ');
Arraylist42.add ('B ');
Arraylist42.add ('C ');
Arraylist42.foo ();
The running result is as follows:
Additional Inheritance Law:
[Class arraylist04]: 2 : A, B
[Class arraylist04]: 3 : A, B, C
Conclusion: The additional inheritance method does not seem like inheritance, but it is the most sexy solution in actual use (PS: this is what our boss calls for good code. Its override is also very clear, as long as in this. extends (baseclass); If a method with the same name is imported into a subclass after a statement, the method imported from the base class is automatically overwritten to achieve override.
Use Cases: anywhere, anytime, anybody...
This seems to be a big case. Perfect things must not exist. Attaching Inheritance Law is also flawed, but this defect does not fall into the inheritance category, it is a problem in the simulation of other OO programming features. We will discuss it later. Let's try again: light is the inheritance and use of classes,Additional Inheritance LawThere is no problem.
Finally, we completed various research on implementing inheritance in JScript simulation object-oriented programming.
The end.