Object-oriented in JavaScript!

Source: Internet
Author: User

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!

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.