JavaScript uses closures to simulate private properties and methods of objects _javascript tips

Source: Internet
Author: User
Tags anonymous closure function definition garbage collection

Recently because of a project, which involves the JS private method, the concept is very common in its language, many languages have private this keyword, as long as the front of a class plus private means that the declaration of a private method, But JavaScript does not have so many features in object-oriented terms, and he doesn't have a special private keyword. To do this, you have to use some of JS's own features to complete the disguise.

First of all, JavaScript has an advanced feature called closure, simple JS closure can be understood as a phenomenon or feature, generally appear in the case of two function nesting, see examples:

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

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

The understanding of closures may not be a telling, here is actually related to a scope of the problem, I remember some people said that the return of the function was C received, C is in the global role, why call C will pop up a function inside the eg, should not be the global scope of the eg? and JS function So local, outside can not access. In fact, there is a theory, remember that the function scope in JS depends on the location of the function definition, not the position of the function call, that is, where the function is defined, his scope is determined, no matter where he calls, the scope will not change, The anonymous function returned is defined in the a function, so his superior scope is this a function, not the global scope.

The private approach to this is actually related to closures, private methods are not accessible in other languages, unless there is a special interface, JS in the local scope of the things in the normal case can not be external access, but the above example shows that the way through the closure can be accessed, So we can take advantage of this feature and see examples:

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

var a = new book ();
a.auther//"Dava"
a.price//
a.page//"wrong"
a._page ()//  100

Here The example uses a function to automate, an anonymous function is executed, and a local variable page is defined in the anonymous function, and then an anonymous function is returned, which is received by the book variable in the global scope, at which point a new object A is generated using the call book. Where the Auther property and the price attribute can be accessed directly from 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 at this point if I want to access the page property, it depends on the closure. The returned function inside the outer layer of the anonymous function, so in the returned function to define a method called _page, this method pop-up page property, according to the JS scope of the relationship, the current scope can not find the page, will go to the upper scope to look for, so found. In this way we distinguish between private methods and public methods.

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.