JavaScript private variable usage

Source: Internet
Author: User

The principle of private instance variables is based on the scope. Private instance variables are implemented using the var keyword in JavaScript Functions and only valid within functions.

If you have been familiar with other languages, you should have been familiar with the concept of private variables. Private is not protected and cannot be modified or accessed. It can only be accessed and modified through the APIS you provide. However, there is no private variable in JavaScript. All members can access and modify it by subscript, for example:

Private variable

The Code is as follows: Copy code

Var F = function (){};

F. prototype. name = "zhangsan ";

Var a = new F ();

Alert (a. name); // zhangsan

A. name = "lisi ";

Alert (a. name); // lisi


In JavaScript, the instance member variables are easily modified and deleted. A lot of people do this.

The Code is as follows: Copy code

Var F = function (){};

// Underline the variable
F. prototype. _ name = "zhangsan ";

Var a = new F ();

// Access is the same as normal access
Alert (a. _ name); // zhangsan

// It can be easily modified.
A. _ name = "lisi ";

Alert (a. _ name); // lisi

It seems that it is common to underline the bit private variables. However, this is only a default convention. In fact, using JavaScript features, we can define private variables as follows:

The Code is as follows: Copy code

(Function (window ){

// Define the default value of the private variable
Var name = "zhangsan ";

Var F = function (){};

// Access private variables
F. prototype. getName = function (){
Return name;
};

// Update private variables
F. prototype. setName = function (str ){
Name = str;
};

Window. F = F;

}) (Window );


Var a = new F ();

A. getName (); // zhangsan

A. setName ("lisi ");

A. getName (); // lisi

This is also the most widely used method.


Private Static variables

The Code is as follows: Copy code


<Script language = "javascript" type = "text/javascript">
Var JSClass = (function (){
Var privateStaticVariable = "nowamagic ";
Var privateStaticMethod = function (){
Alert ("nowamagic ");
};
Return function (){
This. test1 = function (){
Return privateStaticVariable;
}
This. test2 = function (obj ){
PrivateStaticVariable = obj;
}
This. test3 = function (){
PrivateStaticMethod ();
}
};
})();
Var testObject1 = new JSClass ();
Var testObject2 = new JSClass ();
Alert (testObject1.test1 ());
TestObject1.test2 ("change nowamagic ");
Alert (testObject2.test1 ());
TestObject2.test3 ();
</Script>


Dynamic generation of private variable accessors

The Code is as follows: Copy code

 

// Create a new user object and accept an object with many attributes as a parameter
Function User (properties)
{
// Traverse all attributes of the object and ensure that its scope is correct
For (var I in properties ){
(Function (which ){
Var p = I;
// Create a new reader (getter) for this attribute)
Which ["get" + p] = function (){
Return properties [p];
};

// Create a new setter for this attribute)
Which ["set" + p] = function (val)
{
Properties [p] = val;
};
}) (This );
}
}
// Create a new user object instance and pass an object with two attributes as a parameter
Var user = new User ({name: "Bob", age: 44 });
// Read the attribute value
Alert (user. getname ());
// Set the attribute value
User. setage (23 );

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.