/* 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