Effective JavaScript Item 35 uses closures to save private data

Source: Internet
Author: User

This series as Effectivejavascript's reading notes.

JavaScript Object System does not encourage the use of information hiding from its syntax (information hiding) . Because when using this.passwordhash such as this.name, the default access level for these properties is public and can be passed in any position. Obj.name , Obj.passwordhash to access these properties.

in the ES5 environment, there are also ways to make it easier to access all of the properties on an object, such as Object.keys () , object.getownpropertynames () . Therefore, some developers use protocols to define the private properties of JavaScript objects, such as the most typical of which is to use underscores as attributes to tell other developers and users that this property should not be accessed directly.

But doing so does not fundamentally solve the problem. Other developers and users can also have direct access to underlined properties. For situations where private properties are really required, closures can be used for implementation.

in a sense, JavaScript , closures are two extreme for access policies and objects for variables. Any variables in a closure are privately owned by default and can only be accessed within the function. For example, the User type can be implemented as follows:


function User (name, passwordhash) {this.tostring = function () {return "[User] + name +"] ";}; This.checkpassword = function (password) {return hash (password) = = = PasswordHash;};}

at this point, name and the PasswordHash Is not saved as an instance property, but is saved by a local variable. Then, depending on the access rules of the closures, the methods on the instance can access them, but not elsewhere.

one drawback to using this pattern is that methods that take advantage of local variables need to be defined on the instance itself and cannot be defined in prototype object. As discussed in Item34 , the problem with this is that it increases memory consumption. However, in some special situations, it is possible to define a method on an instance.

Summarize:

    1. Variables defined in closures are private and can only be referenced in closures.
    2. Use closures to implement information hiding in methods.

Effective JavaScript Item 35 uses closures to save private data

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.