JS Object-Oriented Programming: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

Recently in the internship company to write code, by the next door Buddy Spit Groove, the code to write a little art. To make my code more artistic, I re-warmed the JavaScript Advanced Programming (several chapters), then looked at the JavaScript design pattern, and then felt the need to write some experience, to sort out what they learned. Here is my personal opinion, wrong please lightly spray, welcome to point out mistakes, willing to correct.

First, the package

(1) Encapsulation in layman's words, I have some secrets that I don't want people to know about, by privatizing variables and privatization methods, so that outsiders can't access them. And then, if you have something that you really want to know about, you can think of the properties created by this as an object common property and object common method, so that people know something about your public, and more than that, you can also access the private properties and private methods of the class or object itself. Wow, this right is so big, the public methods and properties outside, and the internal private properties and methods can all be accessed, all have privileges, so it is called the privileged method. Look at an example and you'll know.

The properties and methods defined on this internal this class can naturally be copied onto newly created objects, become properties and methods that are public to the object, and access private properties and private methods, and are called privileged methods.

That's all you have to call.

(2) package implemented by closures

Closures are functions that have access to variables in another function scope, that is, creating another function inside a function. Closures can then be used as constructors for creating objects so that they are both closures and instances of objects.

Ii. inheritance

(1) class

Each class has 3 parts: 1, which is within the constructor, and is used for instantiating object replication. 2, outside the constructor, is added directly through the point syntax, which is used by the class, and the instantiated object is not accessible. 3, is the prototype of the class, the instantiation of the object can be accessed through its prototype chain, but also for all instantiated objects common.

(2) class-Type inheritance

Through the prototype of the subclass prototype object instantiation to achieve

  

Inheritance is the declaration of 2 classes, but class inheritance requires that an instance of the first class be assigned to a prototype of the second class. This code, when implementing subclass inheritance superclass, is prototype by assigning an instance of superclass to the prototype of subclass, so Subclass.prototype inherits superclass.

The disadvantage is that the common attribute inherited from the parent class constructor of the instance prototype of a subclass directly affects the other subclasses. Like what:

additional points of knowledge : Instanceof is the prototype chain of an object that determines whether the object is an instance of a class, and does not care about the structure of the object and the class itself.

(3) constructor-style inheritance

constructor-style inheritance is accomplished by executing the constructor of the parent class once in the context of the constructor of the subclass.

Superclass.call (This,id) is the essence of constructor-style inheritance, and call can change the environment in which a function is acting. This method of calling the superclass is to execute the variable in the subclass in the parent class again, because the parent class is given the this binding property, so the subclass naturally inherits the common attributes of the parent class. Since this type of inheritance does not involve prototype prototype, the prototype method of the parent class is naturally not inherited by the quilt class, and must be placed in the constructor if you want the quilt class to inherit.

(4) Combination inheritance

Combinatorial inheritance is: Class inheritance + constructor inheritance

Here are the examples to test

As expected, changing the reference type properties inherited by the parent class in the instance of the subclass, such as books, does not affect other instances at all, and can pass parameters to the constructor of the parent class during the subclass instantiation.

(5) prototype-Type inheritance

Prototype inheritance, like class inheritance, attributes of a value type in the parent object book are copied, and the properties of the reference type are shared.

(6) Parasitic inheritance

This is called parasitic inheritance by implementing a transition object within a function that inherits and returns a new object.

parasites, like parasites, are pinned to the inner growth of an object. Is the second package that inherits from the prototype, and the inherited objects are extended during this second encapsulation, so that the newly created objects not only have properties and methods in the parent class but also add new properties and methods.

Look at the following example.

(7) Parasitic combined inheritance

Parasitic combined inheritance is a parasitic inheritance + constructor-type inheritance,

The parent class is created, the prototype method of the parent class is created, the subclass is implemented, the constructor is inherited in the constructor, then the parent prototype is inherited by the parasitic type, and finally some prototype methods are added to the subclass.

Now let's test it.

It is clear that the value of another subclass is changed after the subclass call is not present. One of the biggest changes here is the processing of a subclass prototype, which is given a reference to the parent prototype, which is an object.

(8) Multiple inheritance

By copying inheritance of an object property in this way, the properties and methods of multiple parent classes (objects) are copied to the subclass to implement inheritance

Three, polymorphic

Polymorphism is the realization of a polymorphic processing mechanism by executing the logic by judging the parameters of the transfer.

Here is an example of a polymorphic class that calls the add operation to perform operations on different parameters

This is the object-oriented three characteristics, encapsulation, inheritance, polymorphism, the understanding of the principle, can look at other people's excellent code, there is a good understanding.

JS Object-oriented Programming: encapsulation, inheritance, polymorphism

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.