Javascript is required: face object programming
Core concepts of face object programming technology:Encapsulation, inheritance, and PolymorphismIn some mainstream advanced programming languages, such as: C #, VB. NET, JAVA, PHP, and so on are easy to implement. If you want to implement face-to-face object programming in javascript, it is not so straightforward and easy, because javascript is not an object-oriented language, we can only simulate object-oriented programming through some features of javascript, such as closures and prototype chains, I think these are the foundations for mastering and using javascript flexibly. Many javascript experts in the garden have introduced and analyzed this aspect, I only design and develop WEB Front-end and backend independently as a project owner) to re-understand the main points of javascript object-oriented.
Since it is object-oriented, we first need to know how to create an object. The following lists several common methods for creating an object:
A. directly create an object instance:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Instantiate an object directly var Person1 = { Name: "Dream On the Road", Age: 29, Sex: "Male", Height: 178 }; alert(Person1.Name); var Person2 = new Object(); Person2.Name = "Dream On the Road"; Person2.Age = 29; Person2.Sex = "Male"; Person2.Height = 178; alert(Person2.Name); // This is the abbreviation above var Person3 = new Object({ Name: "Dream On the Road", Age: 29, Sex: "Male", Height: 178 }); alert(Person3.Name); |
Advantage: directly create an object without defining the type in advance;
Disadvantage: it cannot be reused;
B. Define and instantiate the object:
| 1 2 3 4 5 6 7 8 9 10 |
// First define the class and then instance it into an object function Person4(n,a,s,h) { this.Name = n; this.Age = a; this.Sex = s; this.Height = h; } var p4 = new Person4("Dream On the Road", 29, "Male", 178); alert(p4.Age); |
Advantages: constructor similar to object-oriented programming language is easy to understand, and multiple objects can be instantiated through the new Keyword after definition to achieve reuse.
Disadvantage: It must be defined before instantiation;
To sum up, we recommend that you use the B method to create objects.
Implement encapsulation, that is, only public methods and public attributes are exposed, and private methods and attributes are hidden)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function Person5(n, a, s, h) { // Public attributes this.Name = n; this.Age = a; this.Sex = s; this.Height = h; // Public Method this.AfterYear = function (count) { updateAge(count); alert(_currentYear +", I already :" + this.Age +"Years old! "); }; this.Say = function () { alert("My personal information --> Name :"+ this.Name+", Age: "+ this.Age +", Sex: "+ this.Sex +", Height:" + this.Height); } // Private attributes and Methods var _self = this; var _currentYear = 2015; function updateAge(count) { _currentYear += count; _self.Age += count; }; } var p5 = new Person5("Dream On the Road", 29, "Male", 178); p5.AfterYear(10); p5.AfterYear(25); |
Using prototype chain to implement inheritance, that is, an object contains all the public attributes and methods of another object. There are many methods to implement inheritance, I think it is more object-oriented to simulate inheritance in the following form:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function SoftEngineer(n, a, s, h, lang) { Person5.call(this, n, a, s, h);// Include all attributes and methods of Person5 in SoftEngineer to implement inheritance this.Lang = lang; this.SayCode = function () { alert("I am a software engineer, I will" + this.Lang + "Programming language! "); } this.Working = function () { };// Empty method, similar to virtual method in Object-Oriented } SoftEngineer.prototype = new Person5(); // Specify the prototype of SoftEngineer to the Person5 instance var softengr = new SoftEngineer("Dream On the Road", 29, "Male", 178, "javascript"); softengr.Say(); softengr.SayCode(); |
The prototype chain is used to implement polymorphism, that is, signatures based on the same method are displayed in different subclasses in different forms:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function WebSoftEngineer(n, a, s, h, lang) { SoftEngineer.apply(this, [n, a, s, h, lang]); this.Working = function () { alert("I am a web engineer and I am engaged in webpage development and design! "); }; }; WebSoftEngineer.prototype = new SoftEngineer(); function AppSoftEngineer(n, a, s, h, lang) { SoftEngineer.apply(this, [n, a, s, h, lang]); this.Working = function () { alert("I am an application engineer and I am engaged in client application development and design! "); }; }; AppSoftEngineer.prototype = new SoftEngineer(); var webengr = new WebSoftEngineer("Dream On the Road", 29, "Male", 178, "javascript"); webengr.Say(); webengr.Working(); var appengr = new AppSoftEngineer("Dream On the Road", 29, "Male", 178, "c#"); appengr.Say(); appengr.Working(); |