-
- function ClassA(id) {
- this.id = id;
- this.sayId = function() {
- alert(this.id);
- };
- }
- function ClassB(id, name) {
- this.newMethod = ClassA;
- this.newMethod(id);
- delete this.newMethod;
- this.name= name;
- this.sayName= function(){
- alert(this.name);
- };
- }
-
- function ClassB(id, name) {
- ClassA.call(this, id); //this指向ClassB的对象
- this.name = name;
- this.sayName = function() {
- alert(this.name);
- };
- }
-
- function ClassB(id, name) {
- ClassA.apply(this, new Array(id)); //this指向ClassB的对象
- this.name = name;
- this.sayName = function() {
- alert(this.name);
- };
- }
-
- function ClassA(id) {
- this.id = id;
- }
- ClassA.prototype.sayId = function() {
- alert(this.id);
- };
- function ClassB(id, name) {
- ClassA.call(this, id);
- this.name =name;
- }
- ClassB.prototype = new ClassA();
- ClassB.prototype.sayName=function(){
- alert(this.name);
- }
- var o ={}; // o不是对象
- o.eat = function() {return "3432";};
- o.pass = function(text) {this.name = text;};
- var Human = new Function(); //是Function而不是function
- Human.prototype = o;
- var tt = new Human();
使用prototype与apply实现类继承的模拟。用prototype把父类的公有方法与公有变量加入到子类中去。在子类构造方法中,用apply执行父类的构造方法。
- function Parent()
- { //父类
- this.pname="parentName";//父类公有变量
- this.say=function(){alert(this.pname)};//父类公有方法
- this.tell=function(){alert(this.pname+"tell")};//父类公有方法
- }
- function Son()
- {
- //以下两句要先执行,不然不能完成方法覆盖
- Son.prototype=new Parent();//得到父类的公有变量与公有方法
- Parent.apply(this); //调用父类的构造函数
- this.sname="sonName";//子类公有变量
- this.say=function(){alert(this.sname)};//子类公有方法,覆盖了父类的say方法
- this.talk=function(){alert(this.sname+"talk")};//子类公有方法
- }
用js实现自已定义的类体系
- var Person ={ //定义名为Person的类
- Create: function(name, age) {//Create指明构造函数
- this.name = name;//定义类公有变量
- this.age = age; //定义类公有变量
- },
- SayHello: function(){//定义公有方法
- alert("Hello, I'm " + this.name);
- },
- HowOld: function(){//定义公有方法
- alert(this.name + " is " + this.age + " years old.");
- }
- };
- function Fun(){Person.Create("dd","ss");};//定义了一个名为Fun的函数,
- Fun.prototype=Person;//让Fun拥有Person类的成员
- //一旦执行了Fun函数,就执行了Person.Create方法,
- //Person.Create方法一执行,Person就有了公有变量name与age
- //当Person有了公有变量时,由于Fun中有Person类成员,因此Fun也就有了name变量
- //与age变量。
- var p =new Fun();
- p.SayHello();// 显示“Hello, I'm dd ”, 这表明了p具有了变量name与age.
新的基本对象创建
- var TObject = {
- IsA: function (aType)
- {
- var self = this;
- if (self.Type == aType)
- return true;
- self = self.Type;
- while(self)
- {
- if (self.ParentType == aType)
- return true;
- self = self.ParentType;
- };
- return false;
- }
- };
- function Class(aBaseClass, aClassDefine)
- {
- function class_()
- {
- this.ParentType = aBaseClass;
- for(var member in aClassDefine)
- this[member] = aClassDefine[member];
- };
- class_.prototype = aBaseClass;
- return new class_();
- };
- function New(aClass, aParams)
- {
- function new_()
- {
- this.Type = aClass;
- if (aClass.create)
- aClass.create.apply(this, aParams);
- };
- new_.prototype = aClass;
- return new new_();
- };
- Person2 = Class(TObject, {
- T2: 'sdfsd',
- create: function(name, age)
- {
- this.name = name;
- this.age = age;
- },
- sayHello: function()
- {
- alert(this.name + ' ' + this.age);
- }
- });