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

本文介绍JavaScript中通过prototype和apply实现的类继承方法,并展示了如何利用这些技术来自定义类体系,包括构造函数、公有变量及方法的覆盖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    7. functionClassB(id,name){
    8. this.newMethod=ClassA;
    9. this.newMethod(id);
    10. deletethis.newMethod;

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

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

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

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

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

    对象能够执行prototype 属性定义的方法,是因为用构造方法生成的对象和构造方法之间有紧密联系,对象寻找属性时,如果自己没有这个属性,会在构造方法的propotype所指向/引用的对象中找,看能否找到同名属性,如果找到,就会读取它的值并返回.(这个过程会持续向上,直到持续到Object对象为止,即所谓原型方式的继承).
    1. varo={};//o不是对象
    2. o.eat=function(){return"3432";};
    3. o.pass=function(text){this.name=text;};
    4. varHuman=newFunction();//是Function而不是function
    5. Human.prototype=o;
    6. vartt=newHuman();
使用prototype与apply实现类继承的模拟。用prototype把父类的公有方法与公有变量加入到子类中去。在子类构造方法中,用apply执行父类的构造方法。
  1. functionParent()
  2. {  //父类
  3. this.pname="parentName";//父类公有变量
  4. this.say=function(){alert(this.pname)};//父类公有方法
  5. this.tell=function(){alert(this.pname+"tell")};//父类公有方法
  6. }
  7. functionSon()
  8. { 
  9. //以下两句要先执行,不然不能完成方法覆盖
  10. Son.prototype=newParent();//得到父类的公有变量与公有方法 
  11. Parent.apply(this);//调用父类的构造函数
  12. this.sname="sonName";//子类公有变量
  13. this.say=function(){alert(this.sname)};//子类公有方法,覆盖了父类的say方法
  14. this.talk=function(){alert(this.sname+"talk")};//子类公有方法
  15. }
用js实现自已定义的类体系
  1. varPerson={//定义名为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+"yearsold.");
  11. }
  12. };
  13. functionFun(){Person.Create("dd","ss");};//定义了一个名为Fun的函数,
  14. Fun.prototype=Person;//让Fun拥有Person类的成员
  15. //一旦执行了Fun函数,就执行了Person.Create方法,
  16. //Person.Create方法一执行,Person就有了公有变量name与age
  17. //当Person有了公有变量时,由于Fun中有Person类成员,因此Fun也就有了name变量
  18. //与age变量。
  19. varp=newFun();
  20. p.SayHello();//显示“Hello,I'mdd”,这表明了p具有了变量name与age.
新的基本对象创建

  1. varTObject={
  2. IsA:function(aType)
  3. {
  4. varself=this;
  5. if(self.Type==aType)
  6. returntrue;
  7. self=self.Type;
  8. while(self)
  9. {
  10. if(self.ParentType==aType)
  11. returntrue;
  12. self=self.ParentType;
  13. };
  14. returnfalse;
  15. }
  16. };
  17. functionClass(aBaseClass,aClassDefine)
  18. {
  19. functionclass_()
  20. {
  21. this.ParentType=aBaseClass;
  22. for(varmemberinaClassDefine)
  23. this[member]=aClassDefine[member];
  24. };
  25. class_.prototype=aBaseClass;
  26. returnnewclass_();
  27. };
  28. functionNew(aClass,aParams)
  29. {
  30. functionnew_()
  31. {
  32. this.Type=aClass;
  33. if(aClass.create)
  34. aClass.create.apply(this,aParams);
  35. };
  36. new_.prototype=aClass;
  37. returnnewnew_();
  38. };
  39. Person2=Class(TObject,{
  40. T2:'sdfsd',
  41. create:function(name,age)
  42. {
  43. this.name=name;
  44. this.age=age;
  45. },
  46. sayHello:function()
  47. {
  48. alert(this.name+''+this.age);
  49. }
  50. });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值