javascript进阶之对象篇(5)继承

来源:互联网 发布:windows ce是什么 编辑:程序博客网 时间:2024/06/06 07:09
导读:
  {
  for??(property??in??source)???{
  destination[property]??=??source[property];
  }?
  return??destination;
  }?
  function??Man()??{
  this?.name?=?'zkj'}?
  Man.prototype.type?=?'男人'Man.prototype.getType?=?function?()??{
  return???this?.type;
  }?
  function??Woman()??{}?
  Object.extend(Woman.prototype,Man.prototype);
  var??man?=?new??Man();
  var??woman?=?new??Woman();
  alert(man.getType());
  alert(man.name);
  alert(woman.getType());
  alert(woman.name);//?-->??
  我只能说javascript的继承是模拟实现的。和java,c++中是不同的。是依靠prototype实现的。
  我个人从来不用javascript的“继承”,始终认为javascript实现的继承不是真正的继承。可能是受java”毒害“够深。
  在javascript中,我把继承分为两类: 类继承,对象继承。
  (1)、prototype.js中的继承
  prototype.js中用如下代码实现继承。我认为只是个属性拷贝过程。
  Object.extend??=???function?(destination,?source)???{
  for??(property??in??source)???{
  destination[property]??=??source[property];
  }?
  return??destination;
  }?
  //Object.prototype.extend 感觉这句话没必要,太模仿java了,想让对象实例直接继承。
   a、prototype.js中的类继承
  prototype.js??1.3?.?1?
  String.prototype.extend(??{
  stripTags:??function?()???{
  return???this?.replace(?/?]?+>/?gi,?');
  }?,
  escapeHTML:??function?()???{
  var??div??=??document.createElement('div');
  var??text??=??document.createTextNode(?this?);
  div.appendChild(text);
  return??div.innerHTML;
  }?,
  unescapeHTML:??function?()???{
  var??div??=??document.createElement('div');
  div.innerHTML??=???this?.stripTags();
  return??div.childNodes[?0?].nodeValue;
  }?
  }?);
   我把这类型的继承叫做类继承,直接把你自己写的对象属性拷贝到原型对象中去。
  ??

  看了以上代码,可能你会明白。直接拷贝类的原型对象确实可以实现某种概念上的继承。

  但要注意:在继承体系中,Man的原型对象属性方法最好不要用Man的实例属性(name),因为可能Woman中并没有定义实例属性name;也最好不要用Man)原型对象属性字段(type),虽然type也被拷贝过来了,但值还是”男人“。

  虽然有解决办法,但javascript没有很好的语法检查工具,你用prototype.js的类继承时小心处理。

  b、prototype.js中的对象继承

  prototype.js 1.3.1

  this.options = {

   method:?????? 'post',

   asynchronous: true,

   parameters:?? '

   }.extend(options || {});

  这个应用比较简单,典型的对象之间属性拷贝覆盖。

  总结:关于prototype.js中继承的实现,我们当成javascript对象的属性拷贝可能在应用中更好理解。建议大家仔细读读prototype.js代码可能体会更深。模仿prototype.js中extend的应用。感觉var Insertion = new Object(); 的实现比较经典。

  (2)、dojo-0.2.0-ajax中的继承

  dojo.inherits??=???function?(subclass,?superclass)??{

  if?(?typeof??superclass??!=??'?function?')??{?

  dojo.raise(?"?superclass:??"?+?superclass?+?"??borken?"?);

  }?

  subclass.prototype??=???new??superclass();

  subclass.prototype.constructor??=??subclass;

  subclass.superclass??=??superclass.prototype;

  //??DEPRICATED:?super?is?a?reserved?word,?use?'superclass'???subclass['super']??=??superclass.prototype;

  }?

  dojo的继承实现比较正统,也是《javascript权威指南》中的实现方法。注意最后一句代码可以实现子类访问父类原型对象的方法。

      function??Man()??{

  this?.name?=?'zkj'}?

  Man.prototype.type?=?'男人'Man.prototype.getType?=?function?()??{

  return???this?.type;

  }?

  function??Woman()??{}?

  Woman.prototype??=???new??Man();

  Woman.prototype.constructor??=??Woman;

  Woman.superclass??=??Man.prototype;

  //??DEPRICATED:?super?is?a?reserved?word,?use?'superclass'??Woman['super']??=??Man.prototype;

  Woman.prototype.type?=?'女人'?var??man?=?new??Man();

  var??woman?=?new??Woman();

  alert(man.getType());

  alert(man.name);

  alert(woman.getType());

  alert(Woman.superclass.getType());

  alert(woman.name);//?-->??
  看看代码,感觉混乱。
  dojo一直没时间仔细读读代码。这部分详细讨论待续。
  (3)、总结
  关于javascript的继承实现,建议一般不要使用,感觉很乱,代码可读性较差。一般也没有使用必要。详细在《9、javascript对象使用指南》中讨论。

本文转自
http://www.blogjava.net/zkjbeyond/archive/2006/04/17/41532.html
原创粉丝点击