Ajax Getting Started Guide (ii)

Source: Internet
Author: User
Tags object class definition extend implement inheritance object object variable
Ajax starter Guide, which is believed to be helpful to Ajax beginners.

JavaScript Object-oriented programming implementation:
1. Declaration of class:
function Test1 () {
THIS.P1 = "P1";
THIS.P2 = "P2";
THIS.F1 = function () {alert ("F1");}
THIS.F2 = function () {Akert ("F2");}
}
The code above declares two public properties P1 and P2, two methods F1 and F2.
How do I declare a private variable?
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 whose scope is internal to the Test1 class definition and is not externally disclosed.
How do you declare static variables and static methods for a class?
Test1.staticprop = "static Var";
Test1.staticmethod = function () {
Alert ("static method");
}
You can also declare an instance property or method of a class by using the prototype property of the object in javascript:
TEST1.PROTOTYPE.PROP2 = "PROP2";
TEST2.PROTOTYPE.METHOD2 = function () {
Alert ("This.prop2");
}
' Using the prototype property, you can implement a declaration method for another class:
function Test () {}
Test.prototype = {
P1: "P1",
P2: "P2",
F1:function () {
Alert ("F1");
}
}
How do you implement the inheritance of a class? inherits the subclass by copying all the properties and methods of the parent class:
Use for (... in ...) method to traverse all the properties and methods of the parent class.
Let's see how I get the new declared Test1 class to inherit the test class:
function Test1 () {}//defines the Test1 class for a new declaration.
var p;
For (p in Test.prototype) {//Traverse all properties and methods of the parent class
TEST1.PROTOTYPE[P] = test.prtotype[p]; Copy all properties and methods of the parent class to the newly declared subclass Test1
}
Test1.prototype.newMethod = function () {//= new method for defining the new declared subclass Test1
Alert ("New Method");
}
In fact, the prototype framework has helped us achieve this inheritance, and 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 the object object, which has two parameters destination and source, corresponding to subclasses and the parent class, so the Test1 inheritance test that we talked about earlier can simplify the code as follows:
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 preceding code:
function Test1 () {
Test1.prototype = Object.extend (
Test.prototype,
{
Newmethod:function () {
Alert ("New Method");
}
},
);
It can be found that the effect is the same for the Test1 subclass ...
However, we will further find that the parent class test does have a new method of subclass Test1, although this is not the inheritance effect we originally wanted, but this approach gives us the means to extend the object's properties or methods, isn't it?



Related Article

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.