<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
function father1(){
this.age=30;
}father1.prototype.test=function(){
alert('this father1')
}
function father2(){
}
father2.prototype.test=function(){
alert('this father2')
}function son(ss){
this.ss=ss;
   }
 son.prototype = function(){
switch(this.ss){
case 'f1':
 return new father1();
case 'f2':
 return new father2();
default: return this
}
 }
son.prototype.test111 = function(){
alert(2)
}
 var aa=new son('f1');
 aa.test();
 
  </script>
 </HEAD> <BODY>
  
 </BODY>
</HTML>
想通过switch条件来判断继承f1还是f2应该怎么做呢。。上面方法行不通啊 。。 望高手帮帮忙啊。

解决方案 »

  1.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <script>
    function father1(){
    this.age=30;
    }father1.prototype.test=function(){
    alert('this father1')
    }
    function father2(){
    }
    father2.prototype.test=function(){
    alert('this father2')
    }function son(ss){
    this.ss=ss;
    switch(ss){
    case 'f1':
    son.prototype = new father1();
    break;
    case 'f2':
    son.prototype = new father2();
    break;
    default:
    return this;
    }
    }son.prototype.test111 = function(){
    alert(2);
    }
    var aa=new son('f1');
    aa.test();
    </script>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
      

  2.   

    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <script>
    function father1(){
        this.age=30;
    }father1.prototype.test=function(){
        alert('this father1')
    }
    function father2(){
    }
    father2.prototype.test=function(){
        alert('this father2')
    }function son(ss){
        this.ss=ss;
        switch(ss){
        case 'f1':
            son.prototype = new father1();
            return son.prototype;
        case 'f2':
            son.prototype = new father2();
            return  son.prototype;
        default:
            return this;
        }
    }son.prototype.test = function(){
        alert(2);
    }
    var aa=new son('f1');
    aa.test();
    </script>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
      

  3.   

    不行啊  还是不能调用父类的  test方法啊等待解决。。
      

  4.   


    这样只能调用父类的方法了   son.prototype.test111() 不能调用啊 。。郁闷啊。
      

  5.   

    function father1(){
    this.test=function(){
      alert('this father1');
    }
    }
    function father2(){
    this.test=function(){
      alert('this father2');
    }
    } function son(ss){
      switch(ss){
    case 'f1':
    father1.call(this);
    break
    case 'f2':
    father2.call(this);
    break
    default:
    return this;
      }
    }
    son.prototype.test111 = function(){
    alert(2);
    } son.prototype.test=function(){
    alert('son');
    }

    new son("f1").test111();
      

  6.   

    ls的方法只能继承父类的构造方法。。不能继承prototype方法还是谢谢了