您的位置:首页 > Web前端 > JavaScript

JavaScript继承实现方式一览 - winter-cn - 博客园

2008-08-05 11:30 731 查看
导读:
  JavaScript继承实现方式一览
  继承
  类继承
  类抄写
  function parent(){
  this.x=10}function child(){
  var parentObj=new parent();
  for(var p in parentObj)this[p]=parentObj[p];
  }var childObj=new child();
  alert(childObj.x);
  
  类冒充
  
  function parent(){
  this.x=10}function child(){
  this.parent=parent;
  this.parent();
  delete this.parent;
  }var childObj=new child();
  alert(childObj.x);
  
  function parent(){
  this.x=10}function child(){
  parent.call(this);
  }var childObj=new child();
  alert(childObj.x);
  
  原型抄写
  
  function parent(){
  }
  parent.prototype.x=1
  function child(){
  }for(var p in parent.prototype)child.prototype[p]=parent.prototype[p];
  child.prototype.y=2
  var childObj=new child();
  alert(childObj.x);
  
  
  元类继承
  function parent(string){
  var child=new Function("this.x=10;"+string);
  return child;
  }var child=new parent("this.y=20;");var childObj=new child();
  alert(childObj.y);
  
  原型继承
  引用型原型继承
  function parent(){
  this.x=10}function child(){
  }
  child.prototype=new parent();var childObj=new child();
  alert(childObj.x);
  
  复制型原型继承
  function parent(){
  this.x=10}function child(){
  var ret=new parent();
  ret.y=20 return ret;
  }var childObj=new child();
  alert(childObj.x);
  
  posted @ 2008-06-01 22:47 懒得起名阅读(78) 评论(0) 编辑收藏所属分类: JavaScript Language
  2008年6月
  日 一 二 三 四 五 六
  25 26 27 28 29 30 31
  3 4 5 6 7
  8 9 10 12 13 14
  15 16 17 18 20 21
  22 23 24 27 28
  29 30 1 2 3 4 5
  
  与我联系
  发短消息
  留言簿
  给我留言
  
  发现的好东西
  一个免费的.net空间
  朋友的blog或者空间之类的东西
  dron的Ucren
  Rank
  碧原
  风之石
  王好奇
  最新评论
  针对大型系统来讲,OO和设计模式不是不二的法典,构建GUI表示层的HTML/JS也不例外。 (Frank.C)
  winter,貌似未完待续? (dh20156)
  @越兔
  我英文不行 看着勉强可以 翻译就...... (懒得起名)
  >.<
  winter大哥哥,什么时候把ECMA262整体翻译一下啊。。。 (越兔)
  @XeonWell
  这样都可以被发现啊...... (懒得起名)

本文转自
http://www.cnblogs.com/winter-cn/archive/2008/06/01/1211771.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: