Deep understanding of JavaScript impersonating private members

Source: Internet
Author: User

In general, object-oriented languages C + + or Java, objects are private members. There is no class change in JS, nor is the concept of the private member of the object. However, you can simulate a private member by some special notation.

1. Privileged Mode:

(1) variables, sub-functions, and parameters declared inside the constructor are all private to the function and can be considered as private members. the closures that are added to the this pointer are all public members .

so the following example: Parameter A/b, variable _value, function add is private, outside inaccessible, so C1.add will error

SetValue and GetValue are public, and C1 can access

function MyClass (A, b) {//Private    var_value=0; function Add () {_value=a+b; }      //ProcessAdd (); // Public     This. getvalue=function () {return_value; }       This. setvalue=function (A1,B1) {a=A1; b=B1;      Add (); }  }  varc1=NewMyClass (1,2); varC2=NewMyClass (1,2); C1.setvalue (3,4); Alert (C1.getvalue ());//7Alert (C2.getvalue ());//3Alert (C1.add ());//No Add method in C1, errorC1.add is not a function (...)

(2) The advantage of this simulation method is that the code is clear, the disadvantage is that each object's member functions, whether private or public, are unique to their own . even though member functions are functionally identical, they are different functions that are stored in different locations.
(3) This simulation method only mimics a shape. In the OOP language, the member functions of each object are only represented as a unit in memory, and we violate this principle .

2. Private Scope Mode:

(1) since the block-level scope is simulated, consider taking a block-level scope and simulating a private member.

(function () {//Private    var_value=0, _a=0, _b=0; function Add () {_value=_a+_b; }    //constructmyclass=function (A, b) {_a=A; _b=b;        Add (); }      // Public    MyClass.prototype.getValue=function () {return_value; } MyClass.prototype.setValue=function (a1,b1) {_a=A1; _b=B1;      Add ();   }  })(); varc1=NewMyClass (1,2); varC2=NewMyClass (1,2); C1.setvalue (3,4); Alert (C1.getvalue ());//7Alert (C2.getvalue ());//7Alert (C1.add ());//Error

(2) The above example we use block-level scope ,_value, _a, _b, add are private variables of anonymous functions , MyClass Front without Var, all MyClass is a global variable, can be used in the domain of the block-level function ;GetValue and SetValue are public methods that are added to MyClass through the prototype schema .
(3) In this case, thepublic method that MyClass all instances is no longer created separately, but is shared , while the private method is either add in the shared scope chain or created independently . For member methods, it is already very close to C + + and Java.
(4) But the disadvantage of this writing is also obvious , that is, private data members _value, _a, _b are also shared among the instances. These members are equivalent to static members .

3. Integrated mode

(function () {//private static    var_count=0; function Add (A, b) {returnA +b; }      //constructmyclass=function (A, b) {//Private        var_data={Value:0 }          //Process_data.value=Add (A, b); _count++; // Public         This. Data=function () {return_data;} }      // PublicMyclass.prototype.getvalue=function () {return  This. Data (). Value; } MyClass.prototype.setValue=function (A, b) { This. Data (). value=Add (A, b); } MyClass.prototype.getCount=function () {return_count;    }  })(); varc1=NewMyClass (1,2); varC2=NewMyClass (1,2); C1.setvalue (3,4); Alert (C1.getvalue ());//7Alert (C2.getvalue ());//3Alert (C1.getcount ());//2Alert (C2.getcount ());//2

(1) In order to solve the problem of private data member sharing in private scope mode, private data members must be written in MyClass. However, public member functions cannot use them. So it's not possible to use a privileged function that provides an interface to a private data member, which is unavoidable, so it can only be as small as it takes to make a package for a private data member.
(2) Private methods are memory-shared, such as the Add function. In practice, however, private methods only share memory, which is private static state, and private static state method, if you want to use non-parametric, you can only use the private static variables, such as _count. You want to use the private data members of an instance, only through formal parameters.
(3) Public methods are memory-shared and are implemented through the prototype chain. public methods that use private data members must have access to the interfaces of private data members. The this.data we do is the interface .
(4) The whole mode, the number of closures is only one, is this.data, this closure is unavoidable.

Deep understanding of JavaScript impersonating private members

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.