Object-oriented JavaScript---encapsulation

Source: Internet
Author: User

Object-oriented JavaScript---package encapsulation

The purpose of encapsulation is to hide information. In general, the encapsulation we discuss is encapsulated data and encapsulation implementations. The real package is a more generalized package that includes not only encapsulated data and encapsulation implementations, but also package types and package changes.

    • Encapsulating data
    • Encapsulation implementation
    • Package Type
    • Package changes
Encapsulating data

In many language object systems, encapsulated data is implemented by syntax parsing, which may provide different access rights for private, public, protected, and other keywords. But JavaScript does not provide support for these keywords, we can only rely on the scope of the variable to implement the encapsulation feature, and can only simulate both public and private encapsulation.

In addition to the let provided in ECMAScript 6, we typically create scopes through functions:

var myObject = (fucntion(){    var __name = ‘sven‘; // 私有(private变量)    return {        getname: funvtion (){ // 公开(public)方法            return __name;        }    }})();console.log(myObject.getName()); // 输出: svenconsole.log(myObject.__name); // 输出 undefined

It is worth mentioning that, in Ecamscript 6, you can also create a private property through Symbol.

Encapsulation implementation

The purpose of encapsulation is to hide information, encapsulation should be considered "any form of encapsulation", that is, encapsulation is not only hidden data, but also the hidden implementation details, design details and types of hidden objects.

From the encapsulation implementation details, the encapsulation makes the changes inside the object transparent to other objects, which is invisible. The object is responsible for its own behavior. Other objects or users are not concerned with its internal implementation. Encapsulation causes the coupling between objects to become loose, and objects communicate only through exposed API interfaces. When we modify an object, we can arbitrarily modify its internal implementation, as long as the external interface does not change, it will not affect the program's other functions.

There are many examples of encapsulation implementation details. An iterator to illustrate that the role of an iterator is to provide a way to sequentially access the aggregate object without exposing the internal representation of an aggregate object. We've written an each function that iterates through an aggregate object, and the person using the each function doesn't care how it is implemented internally, as long as it provides the right functionality. Even if the each function modifies the internal source code, as long as the external interface or call mode does not change, the user does not care about its internal implementation changes.

Package Type

The encapsulation type is an important encapsulation method in a static type language. In general, encapsulation types are performed through abstract classes and interfaces. After the object's true type is hidden behind an abstract class or interface, the customer is more concerned about the object's behavior than the object's type. In many static language design patterns, finding ways to hide the types of objects is one of the reasons for these patterns to be created. such as factory method mode, combination mode. In JavaScript, of course, there is no support for abstract classes and interfaces. JavaScript itself is a type of vague language. JavaScript has no power and no need to do more in terms of encapsulation types. For JavaScript's design pattern implementations, the non-distinguishing type is a pale, or a relief.

Package changes

From the point of view of design pattern, encapsulation is reflected as package change in more important aspect.
The book "Design pattern" has mentioned the following text:

? "Consider which parts of your design may change, and this is the opposite of the reason why attention can lead to redesign." It's not about when you're going to force your design to change, but about how you can change without being redesigned. The key here is to encapsulate the concept of change, which is the subject of many design patterns. ”

This text is the "design pattern" mentioned in "Find Change and encapsulation." "Design Mode" a book of the CPC summed up 23 design patterns. The 23 design patterns are divided into the following categories:

    • Create pattern
    • Structural mode
    • Behavioral patterns

Creating a pattern: Creating an object is an abstract behavior that creates what objects can be changed, and the purpose of creating patterns is to encapsulate changes in the creation of objects.
Structured mode: Encapsulates a composite relationship between objects.
Behavioral patterns: Encapsulation is the change in the behavior of an object.

In the process of system evolution, we only need to replace those parts that are easy to change, and if the parts are already packaged, it is relatively easy to replace them by encapsulating the changes in the system and isolating the stable parts of the systems from the easily changing ones. This ensures the stability and scalability of the program to the fullest extent possible.

From the "Design pattern" subtitle "The basis of reusable object-oriented software", we know that this book should teach us how to write reusable object-oriented programs. This book puts most of the ink on how to encapsulate changes, which is not contradictory to writing reusable object-oriented programs. When we try to encapsulate the changed parts of the program, the rest is a stable and reusable part.

Object-oriented JavaScript---encapsulation

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.