Javascript static dynamic member and information encapsulation and hiding _ javascript skills

Source: Internet
Author: User
I will use object-oriented concepts to explain the object-oriented imitation in js, because js does not have any static dynamic member related to object-oriented features like other languages.
Next, we will use object-oriented concepts to explain the object-oriented imitation in js. Because JavaScript does not have the object-oriented features like other languages, but some techniques can be used to imitate object-oriented. I used the same concepts to outline the object-oriented concepts of JavaScript, because they acted similarly.

The following functions are available in js:

The Code is as follows:


Function test (){
Var var_value;
This. this_value;
}



Here, var_value is a static member and this_value is a dynamic member.

Static members are associated with the class itself and can only be called at the class level. This feature only keeps one copy of var_value in the memory of the instantiated object and does not occupy too much memory. However, it also has its disadvantages. If it has problems in inheritance, it cannot be derived. Therefore, this method is not recommended if subclass is to be derived.

Dynamic Member, which is associated with the object. This approach is called portal wide-opening. There is a copy of attributes and methods in the memory of each class object. That is to say, how many objects are instantiated and how many memory copies are there. This is not a good thing in terms of efficiency, but it is highly feasible to use it. Although the memory usage is high, some tips can be used to minimize the memory overhead.

For the time being, there may be no space. However, in the future, we will combine more examples for further analysis.

Information encapsulation and hiding

Simply put, it is how to process information. Hiding is the purpose, and encapsulation is the technology to achieve the purpose. Encapsulation can be defined to hide the data representation and implementation details of an object. The member issue mentioned above is also a way of information processing. Here we use these members for encapsulation. Of course, there are other things used.

Generally, encapsulation is based on a wide range of scopes, nested functions, and closures. These concepts have intersection and must be achieved together. It is hard to explain how to encapsulate the data. In the future, more examples will be combined for further analysis.

The advantage of encapsulation is that it maintains the integrity and logic of internal data and imposes restrictions and constraints on variables. Weaken coupling between modules.

The disadvantage is that most of the encapsulated js Code is complicated, because js implementation encapsulation itself is difficult, unlike other languages. This is also a double-edged sword of js flexibility. It is also difficult to debug the encapsulated code, which takes more minutes to deal with the scope and relies on powerful debugging tools. If the program uses a design pattern, it will be hard to understand a programmer who is not familiar with a certain pattern. This requires good code comments or instructions.

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.