js 中的继承

一、对象冒充javascript

<script>java

function Parent(username){ 数组

    this.username = username; app

    this.hello = function(){ 函数

      alert(this.username); this

    } prototype

  } 对象

  function Child(username,password){ 继承

    //经过如下3行实现将Parent的属性和方法追加到Child中,从而实现继承 ip

    //第一步:this.method是做为一个临时的属性,而且指向Parent所指向的对象, 

    //第二步:执行this.method方法,即执行Parent所指向的对象函数及初始化Parent() 

    //第三步:销毁this.method属性,即此时Child就已经拥有了Parent的全部属性和方法 

    this.method = Parent; //把Parent函数做为成员方法

    this.method(username);//执行成员方法

    delete this.method; //构造出来的对象就没有parent了

    this.password = password; 

    this.world = function(){ 

      alert(this.password); 

    } 

  } 

  var parent = new Parent("zhangsan"); 

  var child = new Child("lisi","123456"); 

  parent.hello(); 

  child.hello(); 

  child.world(); 

</script>

二、继承第二种方式:call()方法方式

<script>

  /*

  call方法是Function类中的方法 

  call方法的第一个参数的值赋值给类(即方法)中出现的this 

  call方法的第二个参数开始依次赋值给类(即方法)所接受的参数

  */

  function test(str){ 

    alert(this.name + " " + str); 

  } 

  var object = new Object(); 

  object.name = "zhangsan"; 

  test.call(object,"langsin");//此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str 

 

  function Parent(username){ 

    this.username = username; 

    this.hello = function(){ 

      alert(this.username); 

    } 

  } 

  function Child(username,password){ 

    Parent.call(this,username); 

     

    this.password = password; 

    this.world = function(){ 

      alert(this.password); 

    } 

  } 

  var parent = new Parent("zhangsan"); 

  var child = new Child("lisi","123456"); 

  parent.hello(); 

  child.hello(); 

  child.world(); 

</script>

三、继承的第三种方式:apply()方法方式

<script>

  /*apply方法接受2个参数, 

    A、第一个参数与call方法的第一个参数同样,即赋值给类(即方法)中出现的this 

    B、第二个参数为数组类型,这个数组中的每一个元素依次赋值给类(即方法)所接受的参数 

  */

  function Parent(username){ 

    this.username = username; 

    this.hello = function(){ 

      alert(this.username); 

    } 

  } 

  function Child(username,password){ 

    Parent.apply(this,new Array(username)); 

     

    this.password = password; 

    this.world = function(){ 

      alert(this.password); 

    } 

  } 

  var parent = new Parent("zhangsan"); 

  var child = new Child("lisi","123456"); 

  parent.hello(); 

  child.hello(); 

  child.world(); 

  </script>

四、原型方式继承

<script type="text/javascript">  

    function Person(name,age){  

        this.name=name;  

        this.age=age;  

    }  

    Person.prototype.sayHello=function(){  

        alert("使用原型获得Name:"+this.name);  

    }  

    var per=new Person("zhangsan",21);  

    per.sayHello(); //输出:使用原型获得Name:zhangsan  

    function Student(){}  

    Student.prototype=new Person("lisi",21);  

    var stu=new Student();  

    Student.prototype.grade=5;  

    Student.prototype.intr=function(){  

        alert(this.grade);  

    }  

    stu.sayHello();//输出:使用原型获得Name:lisi  

    stu.intr();//输出:5  

</script>

五、继承的第五种方式:混合方式 

function Parent(hello){ 

    this.hello = hello; 

  } 

  Parent.prototype.sayHello = function(){ 

    alert(this.hello); 

  } 

  function Child(hello,world){ 

    Parent.call(this,hello);//将父类的属性继承过来 

    this.world = world;//新增一些属性 

  } 

  Child.prototype = new Parent();//将父类的方法继承过来 

  Child.prototype.sayWorld = function(){//新增一些方法 

    alert(this.world); 

  } 

  var c = new Child("zhangsan","lisi"); 

  c.sayHello(); 

  c.sayWorld();

相关文章
相关标签/搜索