Javascript abstract class

Source: Internet
Author: User
6.7 implement abstract classes

6.7.1 abstract classes and virtual functions
A virtual function is a concept in a class member. It only serves as a declared but not implemented method. classes with virtual functions are called abstract classes. These virtual functions are implemented only in a derived class. Abstract classes cannot be instantiated because the virtual function is not a complete function and cannot be called. Therefore, abstract classes are generally used only after being derived as base classes.
Similar to class inheritance, JavaScript does not have any mechanism to support abstract classes. However, JavaScript can be used to implement its own abstract classes.

6.7.2 implement abstract classes in Javascript
In traditional object-oriented languages, Virtual Methods in abstract classes must be declared first, but can be called in other methods. In JavaScript, the virtual method can be regarded as a method not defined in the class, but it has been used through the this pointer. Unlike traditional object-oriented methods, virtual methods are directly used without being declared. These methods will be implemented in the derived class, for example:
<Script language = "JavaScript" type = "text/JavaScript">
<! --
// Define the extend Method
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
Object. Prototype. Extend = function (object ){
Return object. Extend. Apply (this, [This, object]);
}
// Define an abstract base class with no Constructor
Function base (){}
Base. Prototype = {
Initialize: function (){
This. oninit (); // a virtual method is called.
}
}
// Define class1
Function class1 (){
// Constructor
}
// Let class1 inherit from the base and implement the oninit Method
Class1.prototype = (new base (). Extend ({
Oninit: function () {// implement the oninit virtual method in the abstract base class
// Oninit function implementation
}
});
// -->
</SCRIPT>
In this way, when the inherited initialize method is called in the class1 instance, The oninit () method in the derived class is automatically executed. The characteristics of interpreted language execution can also be seen here. They will check whether the method exists only when running to a method call, instead of checking whether a method exists in the compilation stage like a compiled language. This problem is avoided in JavaScript. Of course, if you want to add a definition of a virtual method to the base class, you only need to override this method in the derived class. For example:
// Define an abstract base class with no Constructor
Function base (){}
Base. Prototype = {
Initialize: function (){
This. oninit (); // a virtual method is called.
},
Oninit: function () {}// the virtual method is an empty method, implemented by a derived class
}

6.7.3 example of using abstract classes
Taking prototype-1.3.1 as an example, a class creation model is defined:
// Class is a global object with the create method to return a class
VaR class = {
Create: function (){
Return function (){
This. Initialize. Apply (this, arguments );
}
}
}
Here, class is a global object with the create method, which is used to return a function (class) and declare a class. The following syntax can be used:
VaR class1 = Class. Create ();
This is distinguished from the function definition method, so that the Javascript language can have the characteristics of object-oriented language. Now let's look at the returned function (class ):
Function (){
This. Initialize. Apply (this, arguments );
}
This function is also a class constructor, and will be executed when the class is new. It calls an initialize method, which is a class constructor by name. From the class perspective, it is a virtual method and is undefined. However, the implementation of this virtual method is not implemented in the derived class, but defined in prototype after a class is created. For example, prototype can be written as follows:
VaR class1 = Class. Create ();
Class1.prototype = {
Initialize: function (username ){
Alert ("Hello," + username );
}
}
In this way, the initialize method is executed every time a class instance is created, so that the class constructor and class members can be defined together. The following statement is used to pass parameters to the constructor:
Function (){
This. Initialize. Apply (this, arguments );
}
In fact, the arguments here is the parameter passed in function (), that is, the ARGs passed in new class1 (ARGs). Now we need to pass the ARGs to initialize, the apply method of the function is cleverly used. Note that it cannot be written as follows:
This. initialize (arguments );
This is to pass the arguments array as a parameter to the initialize method, while the apply method can pass the element of the arguments array object as a set of parameters. This is a clever implementation.
Although prototype-1.3.1 is not an abstract class concept, it is a design pattern of the class. However, you can actually put the class. the class returned by create () is considered as the common base class of all classes. It calls a virtual method initialize In the constructor. All classes inherited from it must implement this method, complete the functions of the constructor. The essence of their implementation is prototype operations.

 

Reprinted: no real source is found.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.