/* 给一个实例化后的空对象添加属性、方法 */obj = {};obj.Name = "张三";obj.Age = 33;obj.ShowInfo = function () { alert(obj.Name + ", " + obj.Age); }obj.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, 张三, 33obj2 = new MyClass("李四", 44); //实例化obj2.ShowInfo(); //MyClass, 李四, 44
JavaScript 演练(5). 模拟类
TAG: