Recently, I did a project that involved js private methods. This concept is very common in its language. Many languages have the keyword private. As long as private is added to a class, it means that a private method is declared. However, JavaScript does not have so many features in object-oriented aspects, and it does not have a special private keyword. To do this, you must use some of js' own features to complete it in disguise.
First of all, there is a high-level feature in JavaScript called closures. Simply put, the closure of js can be understood as a phenomenon or feature. It usually occurs when two functions are nested, see the example:
function a(){ var eg = 1; return function(){ alert(eg); } } var c = a();
A function returns a function, and the returned function is accepted by c in the global scope. At this time, because the returned function calls the eg variable in function a and is referenced by variable c in the global scope, a closure is formed at this time, and the memory space of function a will not be retracted. The understanding of this closure is actually related to the garbage collection mechanism of js. The garbage collection of js is actually calculated by reference. For example, when we declare a function, the function will have a reference pointing to itself. When the function runs at the end, the reference will be destroyed. If js finds that there is no reference, it will destroy the memory space of the function, and the function will be gone. In our example above, function a is first run, assigning 1 to eg, and then returning an anonymous function. After this function a is finished running, according to the original theory, function a should be destroyed at this time, but at this time it returns a function, which is referenced by the global variable c, and c will not be destroyed unless we manually destroy it. Moreover, the returned function refers to the variable eg of function a, and the js engine will think that eg is still useful because it is still being used, so function a containing the local variable eg, will not be destroyed.
The understanding of closure may not be able to be understood immediately. In fact, there is also a scope issue here. I remember someone said that the returned function was received by c. C is under the global action. Why does the eg in the a function pop up when calling c? Shouldn't it be an eg in the global scope? Moreover, the function of js is local and cannot be accessed externally. In fact, there is a theory here. Just remember that the scope of the function in js depends on the location of the function definition, not the location of the function call. That is to say, where the function is defined, its scope will determine. No matter where it is called, the scope will not change. The anonymous function returned is defined in the a function, so its superior scope is this a function, not the global scope.
The private method we want to talk about here is actually related to closures. Private methods are not accessible in other languages. Unless there is a special interface, the things in the local scope of js cannot be accessed externally under normal circumstances. However, the above example shows that they can be accessed through closures, so we can use this feature to see the example:
var book = (function(){ var page = 100; return function(){ = 'dava'; = 200; this._page = function(){ alert(page); } } })(); var a = new book(); //"dava" // 200 //"wrong" a._page()// 100
This example uses a function to automatically execute. An anonymous function is executed as soon as it comes, and a local variable page is defined in the anonymous function, and an anonymous function is returned and is received by the book variable in the global scope. At this time, calling the book with new will generate a new object a. The auther attribute and price attribute can be accessed directly through the object, because these attributes are directly defined on the returned object when new, while the page attribute does not, so they cannot be reversed. However, if I want to access the page attribute at this time, I have to rely on the closure. The returned function is in the anonymous function on the outer layer, so a method called _page is defined in the returned function. This method pops up the page attribute. According to the relationship between the js scope, if the page cannot be found in the current scope, it will go to the upper scope to search, so it is found. In this way we distinguish private methods from public methods.