1<script type= "Text/javascript" >2 3 //when writing the method code, if this method is only used as a normal method in the future, then follow the "camel Nomenclature", the first letter lowercase, and then the first letter of each word capitalized. 4 //If this method is used to create an object, then the "Pascal nomenclature" is used, and the first letter of each word is capitalized. 5 6 functionPerson () {7 8 }9 Ten //for a method if called directly, then it is considered an ordinary method, One varP=person ();//represents a direct call to the person () method A - varp =NewPerson ();//The person () method, called by the New keyword, represents the creation of an object of type person. - //If you do not have this property, the property is created automatically thep.name = ' Hai bridge '; -P.age = 18; -P.email = ' [email protected] '; - +P.sayhi =function () { -Alert (' I am: ' + This. Name + ', this year: ' + This. Age + ' old, mailbox is: ' + This. email); + }; A at P.sayhi (); - - - - //-----------------Create a person constructor------------------- - functionPerson (name, age, email) { in //This refers to the person - This. User_name =name; to This. User_age =Age ; + This. User_email =email; - the This. Sayhi =function () { *Alert This. user_name + "+ This. User_age + "+ This. User_email); $ };Panax Notoginseng - //add a private member to the current constructor the var_gender; + A //encapsulates two public methods for _gender, a "private" member the This. Get_gender =function () { + return_gender; - }; $ //ways to set values $ This. Set_gender =function(gender) { - if(Gender = = ' Male ' | | gender = = ' female ') { -_gender =gender; the}Else { -Alert (' Sex is wrong! ‘);Wuyi } the }; - Wu } - About $ varMLL =NewPerson (' Zhang San ', ', ' [email protected] '); -Mll.set_gender (' man ')); - - alert (Mll.get_gender ()); A + Mll.sayhi (); the - $ the</script>
Object-oriented in JavaScript!