JavaScript walkthrough (5). Simulation class

Source: Internet
Author: User
/* Add attributes and methods to an empty object after instantiation */obj {}; obj. name & quot; zhangsan & quot; obj. age33; obj. showInfofunction () {alert (obj. name + & quot;, & quot; + obj. age);} obj. showInfo (); // Zhang San, 33 /*...

/* Add attributes and methods to an empty object after instantiation */
Obj = {};
Obj. Name = "James ";
Obj. Age = 33;
Obj. ShowInfo = function () {alert (obj. Name + "," + obj. Age );}

Obj. ShowInfo (); // Zhang San, 33


/* Create and return objects using functions */
Function GetObj (name, age ){
Return {
Name: name,
Age: age,
ShowInfo: function () {alert (this. Name + "," + this. Age );}
}
}

GetObj ("zhangsan", 33). ShowInfo (); // zhangsan, 33


/* Simulation class */
Function MyClass (name, age ){
This. Name = name; // attribute
This. Age = age; // attribute www.2cto.com
This. ShowName = function () {alert (this. Name) ;}; // Method
}

Obj1 = new MyClass ("James", 33); // instantiate
Obj1.ShowName (); // John

// Add attributes and Methods
MyClass. prototype. ClassName = "MyClass ";
MyClass. prototype. ShowInfo = function () {alert (this. ClassName + "," + this. Name + "," + this. Age );}

Obj1.ShowInfo (); // MyClass, Zhang San, 33

Obj2 = new MyClass ("Li Si", 44); // instantiate
Obj2.ShowInfo (); // MyClass, Li Si, 44


From the Delphi blog
 
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.