//implementing object-oriented programming in JavaScriptfunctionPerson (name, age) {//Public Properties This. Name =name; This. Age =Age ; //Public Methods //public methods can access public propertiesPerson.prototype.getName =function () { return This. Name; }; Person.prototype.getAge=function () { return This. Age; }; //In addition to public properties, public methods can also access private propertiesPerson.prototype.getSexOrientation =function () { returnsexorientation; }; //Private Properties varSexorientation = ' heterosexual '; //Private Methods //Private methods only have access to private properties (closures) because in private methods this refers to the Global object window (which is not believed to print), so it is not possible to access the public property through this varSetsexorientation =function(Sex) {sexorientation=sex; //Console.log (this); }; //to test a private method, add a public methodPerson.prototype.admittedByBUPT =function() {Console.log ("I ' m admitted by bupt!"); Setsexorientation (Gay); } //Static PropertiesPerson.country = ' Chinese '; Person.setcountry=function(country) { This. Country =Country; }; Person.getcountry=function () { return This. Country; };}
JavaScript Object-Oriented programming