Javascript Object-Oriented Programming implementation [defining (static) attributes -- inheritance]

Source: Internet
Author: User

Javascript Object-Oriented Programming implementation:
1. Class declaration:
Function test1 (){
This. p1 = "p1 ";
This. P2 = "p2 ";
This. F1 = function () {alert ("f1 ");}
This. F2 = function () {akert ("F2 ");}
}
The code above declares two public attributes P1 and P2, two methods F1 and F2.
How to declare private variables?
Function test1 (){
VaR _ test = "test ";
This. p1 = "p1 ";
This. P2 = "p2 ";
This. F1 = function () {alert ("f1 ");}
This. F2 = function () {akert ("F2 ");}
}
The above Code declares a local Variable _ test through the VaR keyword. Its scope is the internal definition of test1 and is not disclosed to the public.
How do I declare static variables and static methods of a class?
Test1.staticprop = "Static Var ";
Test1.staticmethod = function (){
Alert ("static method ");
}
You can also declare the instance attributes or methods of the class through the prototype attribute of the object in javascript:
Test1.prototype. prop2 = "prop2 ";
Test2.prototype. method2 = function (){
Alert ("This. prop2 ");
}
'Using the prototype attribute, You can implement another type of declaration method:
Function Test (){}
Test. Prototype = {
P1: "p1 ",
P2: "p2 ",
F1: function (){
Alert ("f1 ");
}
}

How can we implement class inheritance? Inherit sub-classes by copying all attributes and methods of the parent class:
Use the for (... in...) method to traverse all attributes and methods of the parent class.
Here let's take a look at how to make the newly declared test1 class inherit the test class:
Function test1 () {}// defines the newly declared test1 class.
VaR P;
For (P in test. Prototype) {// traverses all attributes and methods of the parent class
Test1.prototype [p] = test. prtotype [p]; // copy all attributes and methods of the parent class to test1.
}
Test1.prototype. newmethod = function () {// defines the new test1 method of the new declared subclass.
Alert ("New Method ");
}
In fact, the prototype framework has helped us implement this inheritance. Let's see how it is implemented:
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
The prototype framework defines the extend Method for object objects. This method has two parameters: destination and source, which correspond to subclass and parent class respectively. Therefore, the following code can be simplified for inheriting test1 from test:
Function test1 (){
Test1.prototype = object. Extend ({
Newmethod: function (){
Alert ("New Method ");
}
},
Test. Prototype
);

If we change the order of the two parameters in the extend method in the above Code:
Function test1 (){
Test1.prototype = object. Extend (
Test. prototype,
{
Newmethod: function (){
Alert ("New Method ");
}
},
);
We can find that the effects of test1 subclass are the same ..
However, we will further find that the parent class test does have the new method of the subclass test1, although this is not the inheritance effect we originally wanted, but this method gives us a way to extend object attributes or methods, isn't it?

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.