JavaScript function mode

Source: Internet
Author: User
1 <SCRIPT type = "text/JavaScript">
2 // factory Model
3 function person (name, age, add ){
4 var o = new object ();
5 O. Name = Name;
6 O. Age = age;
7 O. Add = add;
8 o. smay = function (){
9 alert (O. Name );
10}
11 return O;
12}
13 var person1 = person ('xioawang ', 25, 'beijing ');
14 person1.smay ();
15 </SCRIPT>
1 <SCRIPT type = "text/JavaScript">
2 // Constructor
3 function person (name1, age1, Add1 ){
4 This. name1 = name1;
5 This. age1 = age1;
6 This. Add1 = Add1;
7 This. smay1 = function (){
8 alert (this. name1 );
9}
10}
11 var person2 = new person ('xiaoli', 25, 'handand ');
12 person2.smay1 ();
13 </SCRIPT>
1 <SCRIPT type = "text/JavaScript">
2 // prototype
3 function person11 (name2, age2, Add2 ){
4 person11.prototype. name2 = 'xiaozhang ';
5 person11.prototype. age2 = 15;
6 person11.prototype. Add2 = 'hebei ';
7 person11.prototype. saym2 = function (){
8 alert (person11.prototype. name2 );
9}
10}
11 var person22 = new person11 ();
12 person22.saym2 ();
13 var person33 = new person11 ();
14 person33.saym2 ();
15 alert (person11.prototype. isprototypeof (person22 ));
16 </SCRIPT>
1 <SCRIPT type = "text/JavaScript">
2 // simpler Prototype Method
3 function person111 (){
4}
5 person111.prototype = {
6 name222: 'xiaowang ',
7 age222: 25,
8 add222: 'beijing ',
9 smay222: function (){
10 alert (this. name222 );
11}
12}
13 var person333 = new person111;
14 person333.smay222 ();
15 </SCRIPT>

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.