CNET中国旗舰网站

ZDNet China | CNET科技资讯网 | 政府采购 | 行业网站联盟





 
标题: [转贴] 关于prototype继承问题
mz89757053
版主
Rank: 7Rank: 7Rank: 7


UID 304999
精华 0
积分 6933
帖子 8295
威望 116
ZD币 1002 元
阅读权限 250
注册 2008-3-10
状态 离线
  楼主
发表于 2008-3-18 16:02  资料  个人空间  短消息  加为好友 
开发者在线

关于prototype继承问题

《征服Ajax:Web2.0   开发技术详解》中作者介绍了js中类的继承方法:   
  (http://book.csdn.net/bookfiles/33/10033967.shtml)   
  //让class2继承于class1   
  for(var   p   in   class1.prototype){   
  class2.prototype[p]=class1.prototype[p];   
  }   
   
  而prototype.js中类的继承方法是:   
  Object.extend   =   function(destination,   source)   {   
  for   (property   in   source)   {   
  destination[property]   =   source[property];   
  }   
  return   destination;   
  }   
   
  Object.prototype.extend   =   function(object)   {   
  return   Object.extend.apply(this,   [this,   object]);   
  }   
   
  他们有什么不同呢?for(var   p   in   class1.prototype)跟for(var   p   in   class1)的区别是什么?




顶部
热点频道推荐: C/S开发| 数据库| WEB开发| 嵌入式| 项目管理|
mz89757053
版主
Rank: 7Rank: 7Rank: 7


UID 304999
精华 0
积分 6933
帖子 8295
威望 116
ZD币 1002 元
阅读权限 250
注册 2008-3-10
状态 离线
  沙发
发表于 2008-3-18 16:02  资料  个人空间  短消息  加为好友 
测试:   
  <script   language="javascript">   
   
  function   class1(){   
  this.x='xxx';   
  this.y='yyy';   
  this.z='zzz'   
   
  this.mx=function(){alert(this.x)}   
  this.my=function(){alert(this.y)}   
  }   
   
  class1.prototype.a='aaa';   
  class1.prototype.say=function(){alert(z)};   
   
  var   count1=0;   
  var   count2=0;   
  for(var   p   in   class1.prototype){   
  count1++;   
      }   
  for   (var   p   in   class1){   
      count2++;   
      }   
   
  document.writeln('member   count   of   class1.prototype:   '+   count1   +'<br>');   
  document.writeln('member   count   of   class1:   '+   count2   +'<br>');   
   
  </script>   
   
  IE中的结果:   
  member   count   of   class1.prototype:   2   
  member   count   of   class1:   0   
   
  firefox中的结果:   
  member   count   of   class1.prototype:   2   
  member   count   of   class1:   1   
   
  member   count   of   class1.prototype的结果还比较好理解,member   count   of   class1就不明白了。




顶部
热点频道推荐: C/S开发| 数据库| WEB开发| 嵌入式| 项目管理|
 



当前时区 GMT+8, 现在时间是 2009-7-4 14:14

  Powered by Discuz! 5.5.0 © 2001-2007 Comsenz Inc.
Processed in 0.061668 second(s), 3/3 queries

清除 Cookies - 联系我们 - ZDNetChina中文社区 - 无图版