This example describes the simple implementation of JavaScript encapsulation. Share to everyone for your reference. Specifically as follows:
var person = function (name, gender, age) {this
. name = name;
This. Gender = Gender;
This. Age = age;
This. SetName = function (sname) {this
. Name = sname;
}
This. GetName = function () {return this
. Name;
}
This. Setgender = function (sgender) {this
. Gender = Sgender;
}
This. Getgender = function () {return this
. Gender;
}
;
/* Static public method
*
/Person.play = function () {
alert ("This is a static method");
}
/* JavaScript stipulates that each constructor has a prototype property that points to another object. All of the properties and methods of this object are inherited by the instance of the constructor.
This means that we can define the invariant properties and methods directly on the prototype object.
*/
Person.prototype.Address = "People's Republic of China";
Person.prototype.SayHello = function () {
alert (Person.prototype.Address);
}
I hope this article will help you with your JavaScript programming.