Javascript function mode

Source: Internet
Author: User

<Script type = "text/javascript">
// Factory Mode
Function person (name, age, add ){
Var o = new Object ();
O. name = name;
O. age = age;
O. add = add;
O. smay = function (){
Alert (o. name );
}
Return o;
}
Var person1 = person ('xioawang ', 25, 'beijing ');
Person1.smay ();
</Script>
<Script type = "text/javascript">
// Constructor
Function Person (name1, age1, add1 ){
This. name1 = name1;
This. age1 = age1;
This. add1 = add1;
This. smay1 = function (){
Alert (this. name1 );
}
}
Var person2 = new Person ('xiaoli', 25, 'handand ');
Person2.smay1 ();
</Script>
<Script type = "text/javascript">
// Prototype
Function person11 (name2, age2, add2 ){
Person11.prototype. name2 = 'xiaozhang ';
Person11.prototype. age2 = 15;
Person11.prototype. add2 = 'hebei ';
Person11.prototype. saym2 = function (){
Alert (person11.prototype. name2 );
}
}
Var person22 = new person11 ();
Person22.saym2 ();
Var person33 = new person11 ();
Person33.saym2 ();
Alert (person11.prototype. isPrototypeOf (person22 ));
</Script>
<Script type = "text/javascript">
// Simpler Prototype Method
Function person111 (){
}
Person111.prototype = {
Name222: 'xiaowang ',
Age222: 25,
Add222: 'beijing ',
Smay222: function (){
Alert (this. name222 );
}
}
Var person333 = new person111;
Person333.smay222 ();
</Script>

From baikaishui1989
 

Related Article

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.