/* 给一个实例化后的空对象添加属性、方法 */ obj = {}; obj.Name = "张三"; obj.Age = 33; obj.ShowInfo = function () { alert(obj.Name + ", " + obj.Age); } obj.ShowInfo(); //张三, 33 /* 用函数建立并返回对象 */ function GetObj(name, age) { return { Name: name, Age: age, ShowInfo: function () { alert(this.Name + ", " + this.Age); } } } GetObj("张三", 33).ShowInfo(); //张三, 33 /* 模拟类 */ function MyClass(name, age) { this.Name = name; //属性 this.Age = age; //属性 this.ShowName = function () { alert(this.Name); }; //方法 } obj1 = new MyClass("张三", 33); //实例化 obj1.ShowName(); //张三 //补加的属性与方法 MyClass.prototype.ClassName = "MyClass"; MyClass.prototype.ShowInfo = function () { alert(this.ClassName + ", " + this.Name + ", " + this.Age); } obj1.ShowInfo(); //MyClass, 张三, 33 obj2 = new MyClass("李四", 44); //实例化 obj2.ShowInfo(); //MyClass, 李四, 44