JS Privatization Properties

Source: Internet
Author: User

Let's first look at an example:

 var  Demo1 = function   (val) { this . Value = Val;  this . GetValue = function   () { return  this  .value; }    }; var  demo1obj =  //  1  demo1obj.value = 0;alert (Demo1obj.getvalue ());  //  0  

The Demo1 object will have a value property initialized at new. But value itself is not a private property, and we can access the modified property directly by means of '. Value '.

But how do you privatize the Value property?

The main problem here is that the definition of the value variable is directly defined on the property of this object, which allows each Demo1 object to have the Value property.

In fact, we can define value as only exists in object initialization, let value become a local variable in the DEMO1 function, so that other outside the function can not access , and we also give its object to access the interface of the property

Such as:

varDemo1 =function(val) {varValue =Val;  This. GetValue =function(){            returnvalue;      };  This. Increment =function(inc) {value+=Inc; }};varDemo1obj =NewDemo1 (1); alert (demo1obj.value); //0Demo1obj.value = 0;d emo1obj.increment (4); alert (Demo1obj.getvalue ()); //5

In fact, we can use the knowledge of closures,

As shown in the following example:

var function (val) {
var value = val; return {
Increment:function (inc) {
if (typeof inc = = = ' number ') {
Value + = Inc;
}
}, function() { return val;}} }; var New Demo2 (4); alert (Demo2obj.getvalue ()); // 4
alert (demo2obj.val);//undefined

In Demo2, the instantiation of an object is a new structure, and the method in the struct can access the context in which he was created, which is what we call closures . We can call the function GetValue in the closure to get the value of the parameter Val passed in when instantiating.

Note: The value that is accessed here in the closure is not a copy of the property value, but rather itself, so it can be modified in increment.

And the value is not directly outside the external acquisition, which enables the encapsulation of object properties, but also through the form of closures to the appropriate permission to operate.

In summary, the method of privatizing attributes is not to define attributes directly on the this object, but instead to place them in the local variables of the entire function.

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.