博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 中的继承
阅读量:7219 次
发布时间:2019-06-29

本文共 2820 字,大约阅读时间需要 9 分钟。

hot3.png

1、对象冒充

<script>

function Parent(username){ 

    this.username = username; 

    this.hello = function(){ 

      alert(this.username); 

    } 

  } 

  function Child(username,password){ 

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

    //第一步: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>

2、继承第二种方式: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>

3、继承的第三种方式: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>

4、原型方式继承

<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>

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

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();

转载于:https://my.oschina.net/u/1054538/blog/598659

你可能感兴趣的文章
定时与延时任务
查看>>
Squid 日志分析 和反向代理
查看>>
Hadoop的安装及一些基本概念解释
查看>>
大容量分区命令parted
查看>>
从输入 URL 到页面加载完成的过程中都发生了什么事情?
查看>>
实例讲解JQuery中this和$(this)区别
查看>>
centos 7 静态ip地址模板
查看>>
影响系统性能的20个瓶颈
查看>>
shell的详细介绍和编程(上)
查看>>
软件开发性能优化经验总结
查看>>
面试题编程题05-python 有一个无序数组,如何获取第K 大的数,说下思路,实现后的时间复杂度?...
查看>>
kendo grid序号显示
查看>>
Spring 教程(二) 体系结构
查看>>
Indexes
查看>>
2.Web中使用iReport 整合----------创建html格式的
查看>>
异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
查看>>
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
查看>>
初步理解Java的三大特性——封装、继承和多态
查看>>
知识点积累(一)
查看>>
iphone-common-codes-ccteam源代码 CCFile.m
查看>>