JavaScript类继承

<script type="text/javascript">
   //基类 function BaseClass(){ alert('BaseClass'); }
   BaseClass.prototype.hello
= function(){ alert("BaseClass.hello") } //子类 function SubClass(){ alert("SubClass")
     //覆写父类的方法
this.hello = function(){
       BaseClass.prototype.hello.call(this);//调用父类的方法 alert(
"SubClass hello") ; } } //设置子类的基类 SubClass.prototype = new BaseClass();
  //设置子类的构造函数 SubClass.prototype.constructor
=SubClass; window.onload = function(){ var subClass = new SubClass(); subClass.hello();//调用的是子类的hello,如果去掉子类的hello,那么默认调用的是父类的hello } </script>

 

posted @ 2012-12-07 09:57  古月天  阅读(183)  评论(0编辑  收藏  举报