学习javascript对象创建方法(参照别人代码,持续更新)

    1. function ClassA(id) {
    2.     this.id = id;
    3.     this.sayId = function() {
    4.         alert(this.id);
    5.     };
    6. }

    7. function ClassB(id, name) {
    8.     this.newMethod = ClassA;
    9.     this.newMethod(id);
    10.     delete this.newMethod;

    11.     this.name= name;
    12.     this.sayName= function(){
    13.         alert(this.name);
    14.     };
    15. }

    1. function ClassB(id, name) {
    2.     ClassA.call(this, id); //this指向ClassB的对象

    3.     this.name = name;
    4.     this.sayName = function() {
    5.         alert(this.name);
    6.     };
    7. }

    1. function ClassB(id, name) {
    2.     ClassA.apply(thisnew Array(id)); //this指向ClassB的对象

    3.     this.name = name;
    4.     this.sayName = function() {
    5.         alert(this.name);
    6.     };
    7. }
    1. function ClassA(id) {
    2.     this.id = id;
    3. }
    4. ClassA.prototype.sayId = function() {
    5.     alert(this.id);
    6. };
    7. function ClassB(id, name) {
    8.     ClassA.call(this, id);
    9.     this.name =name;
    10. }
    11. ClassB.prototype = new ClassA();
    12. ClassB.prototype.sayName=function(){
    13.     alert(this.name);
    14. }

    对象能够执行prototype 属性定义的方法,是因为用构造方法生成的对象和构造方法之间有紧密联系,对象寻找属性时,如果自己没有这个属性,会在构造方法的propotype所指向/引用的对象中找,看能否找到同名属性,如果找到,就会读取它的值并返回.(这个过程会持续向上,直到持续到Object对象为止,即所谓原型方式的继承).
    1. var o ={};  // o不是对象
    2. o.eat = function() {return "3432";};
    3. o.pass = function(text) {this.name = text;};
    4. var Human = new Function();  //是Function而不是function
    5. Human.prototype = o;
    6. var tt = new Human();

使用prototype与apply实现类继承的模拟。用prototype把父类的公有方法与公有变量加入到子类中去。在子类构造方法中,用apply执行父类的构造方法。
 
  1. function Parent()
  2. {  //父类   
  3.          this.pname="parentName";//父类公有变量   
  4.          this.say=function(){alert(this.pname)};//父类公有方法   
  5.          this.tell=function(){alert(this.pname+"tell")};//父类公有方法   
  6. }
  7. function Son()
  8. {    
  9.              
  10.         //以下两句要先执行,不然不能完成方法覆盖   
  11.         Son.prototype=new Parent();//得到父类的公有变量与公有方法     
  12.         Parent.apply(this); //调用父类的构造函数              
  13.         
  14.         this.sname="sonName";//子类公有变量   
  15.         this.say=function(){alert(this.sname)};//子类公有方法,覆盖了父类的say方法   
  16.         this.talk=function(){alert(this.sname+"talk")};//子类公有方法   
  17. }
用js实现自已定义的类体系
  1. var Person ={ //定义名为Person的类   
  2.       Create: function(name, age) {//Create指明构造函数   
  3.           this.name = name;//定义类公有变量   
  4.           this.age = age; //定义类公有变量   
  5.       },   
  6.       SayHello: function(){//定义公有方法   
  7.           alert("Hello, I'm " + this.name);   
  8.       },   
  9.       HowOld: function(){//定义公有方法   
  10.           alert(this.name + " is " + this.age + " years old.");   
  11.       }   
  12. };   
  13. function Fun(){Person.Create("dd","ss");};//定义了一个名为Fun的函数,   
  14. Fun.prototype=Person;//让Fun拥有Person类的成员   
  15.   
  16. //一旦执行了Fun函数,就执行了Person.Create方法,   
  17. //Person.Create方法一执行,Person就有了公有变量name与age   
  18. //当Person有了公有变量时,由于Fun中有Person类成员,因此Fun也就有了name变量   
  19. //与age变量。   
  20. var p =new Fun();   
  21. p.SayHello();// 显示“Hello, I'm dd ”, 这表明了p具有了变量name与age. 
新的基本对象创建
 

  1. var TObject = {
  2.   IsA: function (aType)
  3.   {
  4.     var self = this;
  5.     if (self.Type == aType)
  6.       return true;
  7.     self = self.Type;
  8.     while(self)
  9.     {
  10.         if (self.ParentType == aType)
  11.             return true;
  12.         self = self.ParentType;       
  13.     };
  14.     return false;  
  15.   }
  16. };
  17. function Class(aBaseClass, aClassDefine)
  18. {
  19.     function class_()
  20.     {
  21.         this.ParentType = aBaseClass;
  22.         for(var member in aClassDefine)
  23.             this[member] = aClassDefine[member];
  24.     };
  25.     
  26.     class_.prototype = aBaseClass;
  27.     return new class_();
  28. };
  29. function New(aClass, aParams)
  30. {
  31.     function new_()
  32.     {
  33.         this.Type = aClass;
  34.         if (aClass.create)
  35.             aClass.create.apply(this, aParams);
  36.     };
  37.     new_.prototype = aClass;
  38.     return new new_();
  39. };
  40. Person2 = Class(TObject, {
  41.     T2: 'sdfsd',
  42.     create: function(name, age)
  43.     {
  44.         this.name = name;
  45.         this.age = age;
  46.     },
  47.     sayHello: function()
  48.     {
  49.         alert(this.name + ' ' + this.age);
  50.     }
  51. });
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值