Private Members in JavaScript (Private member of JavaScript) -- Translation

Source: Internet
Author: User

Most of the Translation results show that some people have translated, and the translation is quite good. However, I tried to flip down my head and make some modifications according to him. This is the first time I translated E.

Original article: http://javascript.crockford.com/private.html

Javascript is the most misunderstood language in the world. Some people think that it lacks the mechanism to hide information, because JavaScript objects do not have private variables and methods. But this is a misunderstanding. Javascript also has private members. Let's take a look.

Object:

Javascript is object-based. Array (arrays) is an object, function (functions) is an object, and object Nima is also an object. What is an object? The object is a set of name-value. The name is a string, and the value can be a string, number, Boolean, and object (including arrays and functions ). Normally, objects are implemented as HashTables so that data can be quickly obtained.

If the value is a function, we can use it as a method. When a method is called by an object, this variable is assigned to this object. This method can obtain the object variables.

Objects can be created through constructors constructor. The constructor provides static variables and methods.

Total members:

All methods of the object are common. Any function can be called, modified, or deleted, or a new method can be added. There are two main ways to add members to a new object:

Using constructors:

This technique is usually used to initialize public object variables. This variable of the constructor is used to add common variables to objects.

function Container(param) {    this.member = param;}

Therefore, if we construct an object

VaR mycontainer = new container ('abc ');

Then mycontainer. member includes 'abc '.

Use prototype

This technique is usually used to add a common method. When a member is found but not found in the corresponding object, it is searched in the portotype of the constructor. The prototype principle is often used to implement inheritance. It also saves memory. If you want to add a method to all object instances, you can add a function in the constructor's prototype to implement:

Container.prototype.stamp = function (string) {    return this.member + string;}

Then, we can call: mycontainer. Stamp ('def ').

'Abcedf' is returned '.

Private member:

Private variables are implemented through constructor. Common var variables and constructor parameters become private members.

function Container(param) {    this.member = param;    var secret = 3;    var that = this;}

This constructor creates three parameters: Param, secret, and that. They can all be accessed inside the object, but cannot be accessed from outside. They can access common methods of objects or private methods. Private methods are internal functions of constructors.

   function dec() {        if (secret > 0) {            secret -= 1;            return true;        } else {            return false;        }    }    this.member = param;    var secret = 3;    var that = this;}

The private method dec checks the instance variable secret. If the value is greater than 0, secret minus 1, and true is returned. Otherwise, false is returned. This method can be used to limit the number of times this object is used. By convention, we define a private variable that. It is used to make the object visible to the private method. This is an error in the ecmascript language specification, which causes this variable to incorrectly replicate internal functions. Private methods cannot be returned by common methods. To make private methods more useful, we need a method: privileged method.

Privileged Methods

Privileged methods can access private variables and methods, and are also visible to common methods. A licensing method may be deleted or replaced, but cannot be modified unless it is completely replaced.

This variable assigned to the constructor by the privileged method

function Container(param) {    function dec() {        if (secret > 0) {            secret -= 1;            return true;        } else {            return false;        }    }    this.member = param;    var secret = 3;    var that = this;    this.service = function () {        return dec() ? that.member : null;    };}

Service is a licensing method. The first three calls to mycontainer. Service () will return 'abc', and then return null. The service method calls the private method DEC to access the private variable Seret. Service is also visible to other objects and methods, but it does not allow direct access to private properties.

Closurs (closure)

The public, private, and privileged modes exist because JavaScript has the closure attribute. The closure here means that internal functions can call external functions even if they have already been returned. This is a very useful feature. There is no book on how to use this feature, and some do not even mention it.

Private and privileged members are only implemented when the object is constructed. Public members can be added at any time.

Writing Mode

Public

function Constructor(...) {         this.membername = value; }Constructor.prototype.membername = value;

Private

function Constructor(...) {         var that =this;         var membername = value;         function membername(...) {...} } 

Note: function declaration function membername (...) {...} is the abbreviation of the following var membername = function membername;

Privileged:

function Constructor(...) {         this.membername = function (...) {...}; }

Thomescai http://blog.csdn.net/thomescai (reprinted please keep)

References:

Http://www.funnyhao.com/test/private-members-in-js.html

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.