/建立唯一类对象的方法: //Person是一个匿名类的唯一实例化 对象, 故它不是一个类, 不能够再用new 来示例化!! var Person={ //注意: 没有 propertype 属性, name:'person', getName:function(){return 'person';} }; //建立类 //方法一: 常规 var One = function(){ this.name = 'One person'; //非静态属性 /*在类里访问属性或方法比须用this*/ this.getName = function(){return this.name;}; //非静态方法 } One.sex = "超女"; //静态属性 One.getSex = function(){return this.sex}; //静态方法 //if(One.prototype) alert("One 有 prototype"); //这样的类含有 prototype 属性 var tmp = new One(); //实例化对象 //alert("姓名 : " + tmp.getName()); //调用非静态方法 //alert("性别 : " + One.getSex()); //调用静态方法 //方法二: 借助prototype.js var MyClass = Class.create(); //创建, 但无任何属性, 方法 Object.extend(MyClass, { //扩展一些静态属性, 方法 //当然也可以用方法一中的方法添加静态属性, 成员函数 qq : "103430585", getQq : function(){return this.qq;} }); Object.extend(MyClass.prototype, { //扩展一些非静态属性, 方法 initialize : function(){ //构造函数, 必须实现且必须为非静态函数, 由Class.create()调用, 可以有参数, alert("MyClass is initializing ..."); }, email : "btpka3@163.com", getEmail : function(){return this.email;} }); //alert(MyClass.getQq()); //调用静态方法, 不能用实例化对象去调用该方法! //var tmp_mc = new MyClass(); //实例化对象 //alert(tmp_mc.getEmail()); //调用非静态方法, 不能用类名去调用该方法!!! var father = Class.create(); Object.extend(father.prototype, { initialize : function(){/*alert("init_father");*/}, say:function(){return "father: say() : nonstatic"; } }); father.say = function(){return "father: say() : static";}; var tmp_f = new father(); //alert("father.say() = " + father.say() +"/ntmp_f.say() = "+ tmp_f.say()); //显示: father.say() = father: say() : static // tmp_f.say() = father: say() : nonstatic var sun = Class.create(); LABEL_MARK: Object.extend(sun.prototype, { // initialize : function(){/*alert("init_sun");*/}, say: function(){return "sun : say() : nonstatic"; } }); Object.extend(sun, { say: function(){return "sun : say() : static";} }); var tmp_s; //以下是在子类已经有自己的静态方法后, 再继承父类后的显示, 四种继承方式中, //在 IE 7.0 与 FF 1.0.7 中 只有第一中的结果不一致, 其他三种均一致! //但是有一个简单的方法可以避免被父类的同名方法覆盖, 那就是 // 先 Class.create(); 再Object.extend()继承父类, //最后再Object.extend()扩充 或 重写 子类的动, 静态方法 /* 第一种: 子类静态属性, 方法继承父类的静态方法 (同名属性被父类覆盖) Object.extend(sun, father); tmp_s = new sun(); alert("after Object.extend(sun, father);/n" + "sun.say() = "+sun.say()+"/n" + "tmp_s.say() = "+tmp_s.say()); */ //显示: after Object.extend(sun, father); // sun.say() = father: say() : static // tmp_s.say() = sun : say() : nonstatic //IE中显示, //最后一句在 FF 中是: father: say() : nonstatic //原因: prototype.js 中的函数 Object.extend() 在IE和FF中的执行不一致!! // FF 比 IE 多了一步: 将 sun[prototype] = father[prototype] /* 第二种: 子类静态属性, 方法继承父类非静态方法,(同名属性被父类覆盖) Object.extend(sun, father.prototype); tmp_s = new sun(); alert("after Object.extend(sun, father.prototype);/n" + "sun.say() = "+sun.say()+"/n" + "tmp_s.say() = "+tmp_s.say()); */ //显示: after Object.extend(sun, father.prototype); // sun.say() = father: say() : nonstatic // tmp_s.say() = sun : say() : nonstatic /*第三种: 子类非静态属性, 方法继承父类静态属性, 方法, (同名属性被父类覆盖) Object.extend(sun.prototype, father); tmp_s = new sun(); alert("after Object.extend(sun.prototype, father);/n" + "sun.say() = "+sun.say()+"/n" + "tmp_s.say() = "+tmp_s.say()); */ //显示: after Object.extend(sun.prototype, father); // sun.say() = sun : say() : static // tmp_s.say() = father: say() : static //但是根据第一种测试知: sun.prototype.prototype = father.prototype; //故 在FF中可以 通过 sun.prototype.prototype.say() 调用父类的 非静态 say()方法; /*第四种: 子类非静态属性, 方法继承父类非静态属性, 方法 (同名属性被父类覆盖) Object.extend(sun.prototype, father.prototype); tmp_s = new sun(); alert("after Object.extend(sun.prototype, father.prototype);/n" + "sun.say() = "+sun.say()+"/n" + "tmp_s.say() = "+tmp_s.say()); */ //显示: after Object.extend(sun.prototype, father.prototype // sun.say() = sun : say() : static // tmp_s.say() = father: say() : nonstatic