Details about JS Object-Oriented Programming and js Object-Oriented Programming
Because JavaScript is based on prototype, and there is no class concept (ES6 has it, so we will not talk about it for the time being), we can access all objects and truly achieve everything as objects.
So let's talk about object blurring. Many people will learn to confuse the concept of type objects and objects. We will not mention objects in the following terms. We will use a similar method as Java to facilitate understanding.
Method 1
Class (function simulation)
Function Person (name, id) {// instance variable can be inherited this. name = name; // Private variables cannot be inherited var id = id; // Private functions cannot be inherited by function speak () {alert ("person1 ");}} // static variables cannot be inherited by Person. age = 18; // public functions can be inherited by Person. prototype. say = function () {alert ("person2 ");}
Inherit and call the parent class Method
Function Person (name, id) {// instance variable can be inherited this. name = name; // Private variables cannot be inherited var id = id; // Private functions cannot be inherited by function speak () {alert ("person1 ");}} // static variables cannot be inherited by Person. age = 18; // a public static member can be inherited by Person. prototype. sex = "male"; // The public static function can be inherited by Person. prototype. say = function () {alert ("person2");} // create a subclass function Student () {}// inherit the personStudent. prototype = new Person ("iwen", 1); // modify the constructor change Student caused by inheritance. prototype. constructor = Student; var s = new Student (); alert (s. name); // iwenalert (s instanceof Person); // trues. say (); // person2
Inherit, rewrite the parent class method and implement super ()
Function Person (name, id) {// instance variable can be inherited this. name = name; // Private variables cannot be inherited var id = id; // Private functions cannot be inherited by function speak () {alert ("person1 ");}} // static variables cannot be inherited by Person. age = 18; // a public static member can be inherited by Person. prototype. sex = "male"; // The public static function can be inherited by Person. prototype. say = function () {alert ("person2");} // create a subclass function Student () {}// inherit the personStudent. prototype = new Person ("iwen", 1); // modify the constructor change Student caused by inheritance. prototype. constructor = Student; // Save the reference var superPerson = Student of the parent class. prototype. say; // method of the parent class Student. prototype. say = function () {// call the superPerson method of the parent class. call (this); alert ("Student");} // create an instance to test var s = new Student (); alert (s instanceof Person); // trues. say (); // person2 student
Inherited encapsulation Functions
function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; }
Uber means to set an uber attribute for the sub-object. This attribute directly points to the prototype attribute of the parent object. (Uber is a German word, meaning "up" and "up ".) This is equivalent to opening a channel on the sub-object. You can directly call the method of the parent object. This line is put here only to realize the completeness of inheritance, which is purely standby.
Method 2
It is equivalent to copying. The defined _ this object is used to carry the object to be inherited. In this way, the inheritance can be implemented by passing _ this, which is better understood than the previous one.
// Create the parent class function Person (name, id) {// create an object to carry all the public objects of the parent class. // that is, the object carrying _ this will be passed to the subclass var _ this = {}; _ this. name = name; // this will not be passed this. id = id; // The bearer method _ this. say = function () {alert ("Person") ;}// return _ this object return _ this ;}// subclass function Student () {// obtain the _ this object of person, and inherit the var _ this = Person ("iwne", 1 ); // Save the _ this reference of the parent class var superPerson = _ this. say; // rewrite the method _ this of the parent class. say = function () {// execute the statement superPerson of the parent class. call (_ this); alert ("Student");} return _ this;} var s = new Student (); s. say ();
I hope it will be helpful for you to learn about javascript programming.
Articles you may be interested in:
- Javascript this details object-oriented
- JS puzzle game object-oriented, complete comments.
- Basic polymorphism of javascript Object-Oriented Programming
- Javascript Object-oriented New Data encapsulation
- JavaScript object-oriented namespace
- Object Usage Analysis of JS Object-Oriented Programming
- Javascript object-oriented (1) (common methods, private methods, privileged methods)
- Javascript seamless scrolling (general method + object-oriented method)
- Basic Introduction to javascript object-oriented