CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  JavaScript

NND,好好的一段代码到我这里怎么就不灵光了呢?

楼主wang2245(初来乍道)2006-12-01 08:59:28 在 Web 开发 / JavaScript 提问

<script   language="javascript"   type="text/javascript">  
  <!--  
  function   class1(){  
  //构造函数  
  }  
  class1.prototype={  
  method:function(){  
  alert(1);  
  },  
  method2:function(){  
  alert("method2");  
  }  
  }  
  function   class2(){  
  //构造函数  
  }  
   
  //让class2继承于class1  
  for(var   p   in   class1.protytype){  
  class2.prototype[p]=class1.prototype[p];  
  }  
   
  //覆盖定义class1中的method方法  
  class2.prototype.method=function(){  
  alert(2);  
  }  
   
  //创建两个类的实例  
  var   obj1=new   class1();  
  var   obj2=new   class2();  
   
  //分别调用obj1和obj2的method方法  
  obj1.method();  
  obj2.method();  
   
  //分别调用obj1和obj2的method2的方法  
  obj1.method2();  
  obj2.method2();             //这里系统提示不支持此属性或方法  
  //-->  
  </script>  
   
  按道理来说运行后的结果应该是1,2,method2,method2   ,但是运行后前三个结果可以得到,第四个结果却得不到,说对象不支持此属性或方法,why?  
  问题点数:20、回复次数:4Top

1 楼zhaoxiaoyang(梅雪香@深圳)回复于 2006-12-01 09:13:32 得分 5

//看一看JavaScript权威指南5th中关于继承的实现  
  //Example   9-3.   Subclassing   a   JavaScript   class  
  //   Here   is   a   simple   Rectangle   class.  
  //   It   has   a   width   and   height   and   can   compute   its   own   area  
  function   Rectangle(w,   h)   {  
          this.width   =   w;  
          this.height   =   h;  
  }  
  Rectangle.prototype.area   =   function(   )   {   return   this.width   *   this.height;   }  
   
  //   Here   is   how   we   might   subclass   it  
  function   PositionedRectangle(x,   y,   w,   h)   {  
          //   First,   invoke   the   superclass   constructor   on   the   new   object  
          //   so   that   it   can   initialize   the   width   and   height.  
          //   We   use   the   call   method   so   that   we   invoke   the   constructor   as   a  
          //   method   of   the   object   to   be   initialized.  
          //   This   is   called   constructor   chaining.  
          Rectangle.call(this,   w,   h);  
   
          //   Now   store   the   position   of   the   upper-left   corner   of   the   rectangle  
          this.x   =   x;  
          this.y   =   y;  
  }  
   
  //   If   we   use   the   default   prototype   object   that   is   created   when   we  
  //   define   the   PositionedRectangle(   )   constructor,   we   get   a   subclass   of   Object.  
  //   To   subclass   Rectangle,   we   must   explicitly   create   our   prototype   object.  
  PositionedRectangle.prototype   =   new   Rectangle(   );  
   
  //   We   create   this   prototype   object   for   inheritance   purposes,   but   we  
  //   don't   actually   want   to   inherit   the   width   and   height   properties   that  
  //   each   Rectangle   object   has,   so   delete   them   from   the   prototype.  
  delete   PositionedRectangle.prototype.width;  
  delete   PositionedRectangle.prototype.height;  
   
  //   Since   the   prototype   object   was   created   with   the   Rectangle(   )   constructor,  
  //   it   has   a   constructor   property   that   refers   to   that   constructor.     But  
  //   we   want   PositionedRectangle   objects   to   have   a   different   constructor  
  //   property,   so   we've   got   to   reassign   this   default   constructor   property.  
  PositionedRectangle.prototype.constructor   =   PositionedRectangle;  
   
  //   Now   that   we've   configured   the   prototype   object   for   our   subclass,  
  //   we   can   add   instance   methods   to   it.  
  PositionedRectangle.prototype.contains   =   function(x,y)   {  
          return   (x   >   this.x   &&   x   <   this.x   +   this.width   &&  
                          y   >   this.y   &&   y   <   this.y   +   this.height);  
  }Top

2 楼zhaoxiaoyang(梅雪香@深圳)回复于 2006-12-01 09:25:21 得分 5

<script   language="javascript"   type="text/javascript">  
  <!--  
  function   class1(){  
  //构造函数  
  }  
  class1.prototype={  
  method:function(){  
  alert(1);  
  },  
  method2:function(){  
  alert("method2");  
  }  
  }  
  function   class2(){  
  //构造函数  
  }  
   
   
          class2.prototype   =   new   class1();  
          class2.prototype.constructor   =   class2;  
  //覆盖定义class1中的method方法  
  class2.prototype.method=function(){  
  alert(2);  
  }  
   
  //创建两个类的实例  
  var   obj1=new   class1();  
  var   obj2=new   class2();  
   
  //分别调用obj1和obj2的method方法  
  obj1.method();  
  obj2.method();  
   
  //分别调用obj1和obj2的method2的方法  
  obj1.method2();  
  obj2.method2();             //这里系统提示不支持此属性或方法  
  //-->  
  </script>Top

3 楼hansonboy(hansonboy)回复于 2006-12-01 10:10:10 得分 5

楼主的代码是可以,但真的有点奇怪,  
   
  for(var   p   in   class1.prototype){  
  class2.prototype[p]=class1.prototype[p];  
  }  
   
  这几句代码我直拷下来不能,   重新自己打一次就可以,Top

4 楼hansonboy(hansonboy)回复于 2006-12-01 10:12:34 得分 5

终于看到原因了,   呵呵  
  for(var   p   in   class1.protytype){  
  楼主你看看你打错什么了?  
  是prototype而不是protytypeTop

相关问题

关键词

得分解答快速导航

  • 帖主:wang2245
  • zhaoxiaoyang
  • zhaoxiaoyang
  • hansonboy
  • hansonboy

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo