JavaScript simulates private properties and methods using closures

Source: Internet
Author: User
Tags static class

Recently, because of a project, which involves JS static methods and private methods, the concept is very common in its language, many languages have static this keyword, as long as a class in front of a static class to declare the declaration of a statically, But JavaScript does not have so many features in object-oriented terms that he does not have a dedicated static keyword, and there is no such access modifier as private in other languages. To do this you have to use JS own some of the features to complete in disguise.

First of all, JavaScript has a high-level feature called closures, simple to say that the closure of JS can be interpreted as a phenomenon or feature, generally appear in the case of two function nesting, see example:

function A () {
var eg = 1;
return function () {
alert (eg);
}
}
var C = A ();


A function returns a function, the returned function is accepted by the global scope of C, at this time because the returned function calls the EG variable inside the A function, and is referenced by the variable C under the global scope, when the closure is formed, the memory space of a function will not be retracted. The understanding of this closure is actually related to JS garbage collection mechanism, JS garbage collection is actually by reference to calculate, for example, we declare a function, this function will have a reference point to himself, when the function is finished to destroy the reference, JS if found not referenced function will destroy the function of memory space , the function is gone. In our example above, we first run a function, give the eg a value of 1, and then return an anonymous function, to this a function ran out, according to the original theory, at this time a function should be destroyed, but at this time he returned a function, this function is referenced by the global variable C, C is not destroyed, unless we manually destroy, And this returned function refers to the variable of a function Eg,js engine will think that eg is still useful because he is still being used, so function A that contains local variables such as eg will not be destroyed.

The understanding of closures may not be a bit of a talk, here is actually related to a scope of the problem, I remember someone said that the return of the function was C received, C is in the global role, why call C when the "a function inside the eg, should not be the global scope of eg?" Moreover, the function of JS is local and cannot be accessed externally. In fact, there is a theory here, remember that the function scope of JS depends on the location of the function definition, not the location of the function call, that is, where the function is defined, his scope is determined, no matter where he is called, the scope will not change, The anonymous function returned is defined within the a function, so his ancestor scope is the A function, not the global scope.

Here to say the private method is actually related to the closure, the private method in other languages is not access to, unless there is a dedicated interface, JS local scope inside the things under normal circumstances can not be external access to, but the above example shows that through the way of closures can be accessed, So we can take advantage of this feature to see examples:


var book = (function () {
var page = 100;
return function () {
This.auther = ' Dava ';
This.price = 200;
This._page = function () {
alert (page);
}
}
})();


var a = new book ();
a.auther//"Dava"
a.price//"200"
a.page//"Wrong"
a._page//"100"
In this example, a function is executed automatically, an anonymous function is executed, and a local variable page is defined inside the anonymous function, then an anonymous function is returned, and is received by the book variable under the global scope, and a new object A is generated using new. Where the Auther property and the price property can be accessed directly through the object, because these properties are defined directly on the returned object when new, and the page property is not, so it cannot be reversed, but if I want to access the page property, then I have to rely on the closure, Functions returned in the outer layer of the anonymous function inside, so in the returned function defines a method called _page, this method pops up the page property, according to the JS scope of the relationship, the current scope can not find the page, it will go to the upper scope to find, so we found. In this way we distinguish between private methods and public methods.

JavaScript simulates private properties and methods using closures

Related Article

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.