1. What is a private property
In JavaScript code, there are no keywords that define attributes such as public/protected/private, but we can do this in a simulated way.
There are only two types of properties in javascript:
A common attribute
A private property
2. Define private properties
Inside the constructor, the property defined by this is the public property
Within the constructor, a property defined by Var is a private property
<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body><Script>functionRen () { This. Name=' Xiaohong';//Use this to define the public property var age = ' 50′; Use Var to define private properties}//instantiate Shuo object var Shuo = new Ren (); alert (shuo.name); Xiaohongalert (Shuo.age); Undefined</script></body>
3. Assignment and access of private attributes
In some cases, we might run the need to assign and access private properties, so how do we do that?
<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body><Script>functionRen () { This. Name=' Xiaohong';//Use this to define the public attribute Var age; Use Var to define private properties//define a public method to implement access to the Age property This.geterzi = function () {alert (age);} Defines a public method for the Age property copy Operation This.seterzi = function (Jiang) {age = Jiang;}} Instantiate Shuo object var Shuo = new Ren (); alert (shuo.name); Xiaohongalert (Shuo.age); Undefinedshuo.seterzi (' n '); Shuo.geterzi ();</script></body>
If a private property has only a Get method, then it is a readable property, and if a private property has only one set method, then it is a writable property, both of which have a readable writable property.
Kill lui lei dog---javascript no:31 private property