This article describes the JavaScript simple implementation object-oriented programming inheritance instance code. Share to everyone for your reference, specific as follows:
An object-oriented language must have four basic characteristics:
1. Encapsulation capability (that is, allowing a variable or function of a basic data type to be placed in a class to form a member or method of a class)
2. Aggregation capability (that is, allow classes to be included in the class so that it can cope with complex designs)
3. Support Inheritance (a parent class can derive a subclass, a subclass with a parent's property or method)
4. Support polymorphism (allow the same method name, according to the method signature [i.e. function parameters] different, have their own independent processing method)
These four basic properties, JavaScript can support, so JavaScript is really a weak type of object-oriented language, here gives a simple class inheritance code
<script type= "Text/javascript" >
//Parent class ClassA
function ClassA (scolor) {
this.color = Scolor;
This.saycolor = function () {
document.write ("Color:" + this.color + "<br/>");
}
Subclass ClassB, inheriting from ClassA function
ClassB (scolor,sname) {
classa.call (this,scolor);//using the Call function, All the methods of ClassA are assigned to CLASSB, that is, to realize the inheritance
this.name = sname;
This.sayname = function () {
document.write ("Name:" + this.name + "<br/>");
}
var Oclassa = new ClassA ("Red");
Oclassa.saycolor ();
var oclassb = new ClassB ("Blue", "Jimmy.yang");
Oclassb.saycolor ()///Here the Saycolor method is inherited from the ClassA
oclassb.sayname ();//This is the new method in CLASSB/
*
Demo Example of the Call function
function Saycolor (Sprefix, ssuffix) {
alert (sprefix + This.color + ssuffix);
var obj = new Object ();
Saycolor.call (obj, "The color is", ", a very nice color indeed.");
* *
</script>
I hope this article will help you with JavaScript programming.