Javascript creates private and static Private Members

Source: Internet
Author: User

Javascript creates private and static Private Members
Private methods and attributes

In javascript, because there is no concept of a class, classes can only be simulated by constructors. If you need to write a mobile phone class, you need to input a phone number and then call the phone. The simplest way is

Var Phone = function (num) {// mobile Phone number this. num = num; // call this. call = function (phone) {console. log (this. num + calling + phone. num) ;}} var p1 = new Phone (13012341234 );

For data correctness, we may also check its validity when assigning values to num, for example

Var Phone = function (num) {if (! This. checkNum (num) {throw new Error (the mobile phone number is invalid !); } // Mobile phone number this. num = num; // call this. call = function (phone) {console. log (this. num + calling + phone. num) ;}} // verify the validity of the mobile Phone number. prototype. checkNum = function (num) {if (/^ 1 [358] d {9} $ /. test (num) {return true;} else {return false;} var p1 = new Phone (13312341234); var p2 = new Phone (15812341234); p1.call (p2 ); // 13312341234 calling 15812341234

This situation will be much better, but although the num is controlled when the object is created, but when this code is used by other programmers, he may directly assign values to the mobile phone, such

p1.num = abcdefg;p1.call(p2);

Even if we can verify the integrity of data in the constructor, we still cannot control the attributes to be arbitrarily modified. In this case, we can use the closure to change num to a private variable, to access num, you can only use the get and set methods.

Var Phone = function (num) {// Private variable var _ num; // privileged method this. setNum = function (num) {if (! This. checkNum (num) {throw new Error (the mobile phone number is invalid !); } _ Num = num;}; this. getNum = function () {return _ num;}; this. call = function (phone) {console. log (_ num + calling + phone. getNum () ;}; // initialize this. setNum (num);} Phone. prototype. checkNum = function (num) {if (/^ 1 [358] d {9} $ /. test (num) {return true;} else {return false;} var p1 = new Phone (13312341234); var p2 = new Phone (15812341234); p1.call (p2 ); // 13312341234 calling 15812341234

Attributes or methods like num are declared with the var keyword, rather than this, which means they can only be used within the function. Therefore, a private attribute is formed, and private methods are similar, directly declare a function in the constructor as a private method. Private methods cannot be directly accessed from outside, but can be accessed through functions such as setNum and getNum through the closure feature. This public method that can access private attributes is called a privileged method.
However, this method also has drawbacks in object creation. The call function calls the private attribute num, so it can only be placed in the constructor, and too many privileged methods usually occupy too much memory, every time a new object instance is generated, a copy is generated for each private and privileged method, which consumes more memory than the original method.

Static methods and attributes

Static members are related to classes rather than instances. That is to say, static members are directly accessed through classes, and all instances share a static member. It is very easy to achieve a common static method, you only need to assign a value to the constructor, as shown in figurePhone.count = 5;However, Private Static members cannot be directly accessed by external entities. Therefore, closure is required:

Var Phone = (function () {// static private property var count = 0; return function (num) {// Private variable var _ num; // privileged method this. setNum = function (num) {if (! This. checkNum (num) {throw new Error (the mobile phone number is invalid !); } _ Num = num;}; this. getNum = function () {return _ num;}; this. call = function (phone) {console. log (_ num + calling + phone. getNum () ;}; // no count ++ is created for more than 50 mobile phones; if (count> 50) {throw new Error (only 50 mobile phones can be created !); } // Initialize this. setNum (num) ;}}) () Phone. prototype. checkNum = function (num) {if (/^ 1 [358] d {9} $ /. test (num) {return true;} else {return false;} var p1 = new Phone (13312341234); var p2 = new Phone (15812341234); p1.call (p2 ); // 13312341234 calling 15812341234

It is similar to the method created above, but the constructor here becomes an embedded function and is returned through immediate execution of external functions, this enables the count of the external function to be accessed by the constructor. Because the external function is loaded only once, there is only one count. When 50 instances are created, count is assigned to 50. If you continue to create the instance, an error is returned.

 

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.