Although the topic covered in this article is very basic and considered a trick by many people, it is a comprehensive topic in the basic knowledge of JavaScript. This will involve knowledge of object attribute encapsulation, prototypes, constructors, closures, and immediate execution of expressions .
Public methods
Public methods are methods that can be accessed and called externally.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Private methods and Privileged methods
These two methods are generally discussed together because the privileged method we define refers to the public method that has access to internal private properties and private methods, while the private method refers to the external invisible and inaccessible method. method.
There are usually two ways to define an object, one is to use Object instantiation or object expression, and the other is to use a constructor. Similarly, the forms of defining private methods and privileged methods are also different in different ways.
in object
Here we use Object expression to create an object and add some properties and methods, and then call it directly in a static way. The object's private data is placed in an anonymous function immediate execution expression (IIFE). This means that this function only exists at the moment it is called, and is destroyed immediately after execution.
The way to create private data in an object is called the module pattern in the object mode (referring to the mode of creating objects). Its basic format is as follows:
1 2 3 4 5 6 7 8 |
|
In module mode, the returned object literal only contains properties and methods that can be exposed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Note that we use closures to indirectly use internal private variables and initialize the name of the restaurant.
In constructor
When creating private methods in the module pattern introduced above, there is no essential difference between public methods and privileged methods. The reason is that this concept is defined when using constructors to create private data.
It is convenient to define private properties and methods in the constructor. We do not need to use closures and can initialize the data when calling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Two in one, more flexible way
Using the module pattern we can call it multiple times and it will be destroyed after each execution. Some initialized data can be passed in using the constructor method, but private member properties cannot be accessed in public methods. If there are many public methods that need to access private data, we will write them all in privileged methods, and finally bring them to each instance. Many unnecessary methods. Therefore, combining the two can complement each other, and the combination method is also very simple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
The above is the entire content of this article. The editor has only summarized a small part of it. There are still many knowledge points that have not been mentioned. You can explore and study by yourself. I hope this article can be helpful to beginners.