Javascript Object-Oriented Programming Skills

Source: Internet
Author: User

Javascript will undoubtedly become an important programming language after HTML5 comes. With the adoption of the Javascript 2.0 specification, Javascript has become more and more essential for a powerful programming language. In this regard, object-oriented programming is an important aspect.
Object-oriented has four fields: abstraction, encapsulation, inheritance, and polymorphism. The following describes how these concepts are implemented in Javascript. Note: Due to the original design intention and application scope of Js, Js itself does not provide the OO structure, and the implementation of OO is more accomplished by using the dynamic mechanism of Js.
The first is abstraction. In OO, classes are used to describe objective things. For example, a student can be regarded as a class. The class has attributes such as name, gender, and age, and has two methods: Admission and graduation. Every student is an instance of this class. However, in Javascript, we can use functions to simulate classes without class language elements.
Function Student (name, gender, age ){
This. name = name;
This. gender = gender;
This. age = age;
}

Student. prototype. register = function (){...};

Student. prototype. graduate = function (){....};

The student class defined in the Code above has three public attributes and two public methods. The usage is as follows:

Var st = new Student ();
St. name = "Yantao ";
St. register ();

Secondly, the important concept in OO is encapsulation. We expose public interfaces of classes to callers, but we use private methods for internal implementation-related attributes and methods to make them invisible to external callers. Private attributes and private methods can be implemented in Javascript using the following syntax:

Function Student (_ code ){
This. getCode = function () {// privileged function, which can obtain private attributes and methods and can be called externally.
Return code;
};

This. setCode = function (_ code ){
Code = _ code;
}

Var code; // Private Property
Code = _ code;

Function manage () {// Private method}
}

Var st = new Student ();
St. setCode ("abc ");

As described above, information encapsulation can be implemented through private attributes and methods, and external interfaces can be implemented through privileged functions. Note: Only public functions can obtain public attributes and methods.
The next step is inheritance. You can create a new class based on the existing class and extend the existing class. There are two methods in Javascript to implement inheritance, one is the function method, and the other is the prototype method. Because the prototype can dynamically extend the inheritance base class at runtime, we only discuss prototype-based inheritance here.

// First define the base class
Function Person (){
This. getName = function (){
Return name;
}

This. setName = function (_ name ){
Name = _ name;
}

Var name = null;
}

Person. prototype. dumpObj = function (){
Return "Person. dumpObj ";
}

 

Function Student (){
This. getSchool = function () {return school ;}

This. setSchool = function (_ school) {school = _ school ;}

Var school = null;
}

Student. prototype = new Person;
Var p = new Person ();
P. setName ("Yantao ");
Alert ("Person:" + p. getName ());
Var s = new Student ();
S. setSchool ("Huilongguan ");
S. setName ("Yan ");
Alert ("student:" + s. getSchool () + ":" + s. getName () + "! ");

There are two types of polymorphism: one is that the same function name has different behaviors due to different parameter types and numbers. This behavior is called Overloading, and the other is the same function name and parameter, the method in the subclass overwrites the method in the parent class. This behavior is called Overriding.
If different parameters have different behaviors, use the following code:
Student. prototype. showFunc = function (){
If (1 = arguments. length ){
Alert ("one argument! ");
}

If (2 = arguments. length ){
If (Number = arguments [0]. constructor ){
Alert ("Two: number ");
} Else if (String = arguments [0]. constructor ){
Alert ("Two: string ");
}
}
}

Var s = new Student ();
S. showFunc ("abc ");
S. showFunc (20, "AB ");
S. showFunc ("20", 22 );

You can use the following code to implement a subclass override parent class method:
Function Person (){
This. printObj = function (){
Return "I am Person! ";
}

This. getName = function (){
Return name;
}

This. setName = function (_ name ){
Name = _ name;
}

This. modifyName = function (){
Return "name =" + name + "! ";
}

Var name = null;
}

Person. prototype. dumpObj = function (){
Return "Person. dumpObj ";
}

 

Function Student (){
This. printObj = function (){
Var selfStr = "I am Student! "+ This. dumpObj () + this. modifyName () + Student. prototype. printObj ();
Return selfStr;
}

 

This. getSchool = function (){
Return school;
}

This. setschool = function (_ school ){
School = _ school;
}

VaR school = NULL;
}

Student. Prototype = new person;
Student. Prototype. showfunc = function (){
If (1 = arguments. Length ){
Alert ("one argument! ");
}

If (2 = arguments. Length ){
If (number = arguments [0]. constructor ){
Alert ("Two: Number ");
} Else if (string = arguments [0]. constructor ){
Alert ("Two: string ");
}
}
}

Var s = new Student ();
// Var p = (Person) s;
Alert (s. printObj ());

 

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.