Home > Web Front-end > JS Tutorial > body text

js static dynamic members and information encapsulation and hiding_javascript skills

WBOY
Release: 2016-05-16 18:06:17
Original
1104 people have browsed it

Static dynamic members
Let’s use object-oriented related concepts to explain the imitation object-oriented in js, because unlike other languages, js does not have the relevant features of object-oriented languages, but it can imitate object-oriented through some techniques. I use the same concepts to outline these aspects of JavaScript as object-oriented concepts, because they behave similarly.

There are the following functions in js

Copy code The code is as follows:

function test(){
var var_value;
this.this_value;
}


Among them, 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 is that only one copy of var_value is kept in the memory in the instantiated object and does not occupy too much memory. But it also has shortcomings. If it is derived in terms of inheritance, there will be problems and it cannot be derived. Therefore, if you want to derive a subclass, this method should not be used.

Dynamic members, which are associated with objects. This approach is called the open-door approach. In each object of this class, there is a copy of the properties and methods in the memory. That is to say, there are as many memory copies as there are objects instantiated. This is not a good thing in terms of efficiency, but its practical feasibility is relatively high. Although it takes up a lot of memory, you can also use some techniques to reduce the memory overhead as much as possible.

That’s all I’ll talk about for now, maybe I’ll have some time. However, in future essays, more examples will be combined for a deeper analysis.

Encapsulation and hiding of information

To put it simply, it is how to process information. Hiding is the purpose, and encapsulation is the technology used to achieve the purpose. Encapsulation can be defined as hiding the data representation and implementation details inside an object. The member problem mentioned above is also a way to process information. Here is how to use these members to encapsulate. Of course, there are other things used.

The commonly used basis for encapsulation are: scope, nested functions, and closures. These concepts overlap and must be used together to achieve the goal. As for how to encapsulate it specifically, it is difficult to explain. In future essays, more examples will be combined for a deeper analysis.

The advantages of encapsulation are: it maintains the integrity and logic of internal data, and it imposes restrictions and constraints on variables. Weaken the coupling between modules.

The disadvantage is: most of the encapsulated js code is complicated, because it is difficult to implement encapsulation in js, unlike other languages. This is also the double-edged sword of js flexibility. It is also more difficult to debug encapsulated code, which requires more time to deal with scopes and relies on powerful debugging tools. If the program uses a design pattern, it will be difficult to understand for a programmer who is not familiar with a certain pattern. This requires the help of good code comments or documentation.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template