The core concept of surface Object Programming Technology: encapsulation, inheritance, polymorphism; In some major high-level programming languages, such as: c#,vb.net,java,php, etc. are easy to implement, and if you want to implement polygon object programming in JavaScript, It's not so straightforward and easy, because JavaScript is not an object-oriented language, so we can only simulate object-oriented programming through some JavaScript features, such as closures, prototype chains, and so on, which I think are the basis for mastering and using JavaScript flexibly. , there are a lot of javascript gurus in the garden that are introduced and analyzed in this area, and I am only re-understanding JavaScript Object-oriented points as a project leader (independently designed and developed web front-end and back-end).
Since it is object-oriented, we first need to know how to create an object, and here are a few common ways to create an object:
A. Create an object instance directly:
Instantiate an object directly var Person1 = {Name: "Dream On Journey", Age:29, Sex: "Male", height:178};alert (person1.name); var Person2 = new Object ();P Erson2. Name = "Dream On the Journey"; person2.age = 29; Person2.sex = "male"; Person2.height = 178; alert (person2.name);//This is the abbreviated var Person3 = new Object ({Name: "Dream On Journey", Age:29, Sex: "Male", height:178}); alert (Person3.nam e);
Pros: Create an object directly without having to define the type in advance;
Disadvantage: unable to achieve reuse;
B. Defining a post-instantiation object first:
The class is first defined and then instantiated into 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 Journey", 29, "male", 178); alert (P4. Age);
Pros: Similar to object-oriented programming language constructors, easy to understand, and after the definition can be instantiated through the New keyword multiple objects, to achieve reuse.
Disadvantage: Need to define before you can instantiate;
In summary, it is recommended to use the B method to create objects.
Implement encapsulation, which exposes only public and public properties, and hides implementation details (private methods, properties)
function Person5 (n, a, S, h) { //public Property this . Name = n; This. age = A; This. Sex = s; This. Height = h; Public method This . Afteryear = function (count) { updateage (count); Alert (_currentyear + "After I have:" + this. Age + "years old!" "); }; This. Say = function () { alert ("My personal information--and Name:" + this.) Name+ ", Age:" + this. Age + ", Sex:" + this. Sex + ", Height:" + this. Height); } Private property and Method var _self = this; var _currentyear =; function Updateage (count) { _currentyear + = count; _self. Age + = count;} ; var P5 = new Person5 ("Dream On Journey", 29, "male", 178);p 5. Afteryear;p 5. Afteryear (25);
Using a prototype chain to implement inheritance, that is, one object contains all the public properties and methods of another object, there are many ways to implement inheritance, and I think the following form is used to simulate inheritance more consistent with object-oriented thinking:
function Softengineer (n, a, S, h, Lang) { Person5.call (this, N, a, S, h);//All properties and methods of Person5 are included in Softengineer to achieve inheritance C1/>this. lang = lang; This. Saycode = function () { alert ("I am a software engineer, I will" + this.) Lang + "programming language! "); } This. Working = function () {};//null method, similar to virtual method in Object-oriented}softengineer.prototype = new Person5 (); The Softengineer prototype is specified as an instance of Person5 var softengr = new Softengineer ("Dream On Journey", 29, "Male", 178, "JavaScript"); Softengr. Say (); Softengr. Saycode ();
The use of the prototype chain to achieve polymorphism, that is, based on the same method signature in different subclasses behave in different forms:
function Websoftengineer (n, a, S, h, Lang) { softengineer.apply (this, [N, A, S, h, Lang]); This. Working = function () { alert ("I am a Web engineer, engaged in web development and design work!") "); };}; 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, engaged in client application development design work!") "); };}; Appsoftengineer.prototype = new Softengineer () var webengr = new Websoftengineer ("Dream On Journey", 29, "Male", 178, "JavaScript"); Webe NgR. Say (); Webengr. Working (); var appengr = new Appsoftengineer ("Dream On Journey", 29, "Male", 178, "C #"); Appengr. Say (); Appengr. Working ();
JavaScript must know: Surface Object programming