js继承有5种实现方式: 
1、继承第一种方式:对象冒充 
  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所指向的对象函数 
    //第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法 
    this.method = Parent; 
    this.method(username);//最关键的一行 
    delete this.method;

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

2、继承第二种方式:call()方法方式 
  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();

3、继承的第三种方式:apply()方法方式 
  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();

4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承 
  function Person(){ 
  } 
  Person.prototype.hello = "hello"; 
  Person.prototype.sayHello = function(){ 
    alert(this.hello); 
  } 
  
  function Child(){ 
  } 
  Child.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承 
  Child.prototype.world = "world"; 
  Child.prototype.sayWorld = function(){ 
    alert(this.world); 
  } 
  
  var c = new Child(); 
  c.sayHello(); 
  c.sayWorld();

5、继承的第五种方式:混合方式 
  混合了call方式、原型链方式

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

js继承的实现的更多相关文章

  1. js继承

    js继承有5种实现方式: 继承第一种方式:对象冒充 function Parent(username){ this.username = username; this.hello = function ...

  2. js继承之call,apply和prototype随谈

    在js中,call,apply和prototype都可以实现对象的继承,下面我们看一个例子: function FatherObj1() { this.sayhello = "I am jo ...

  3. js继承精益求精之寄生式组合继承

    一.混合/组合继承的不足 上一篇JS继承终于混合继承,认真思考一下,发现其还是有不足之处的: 空间上的冗余:在使用原型链的方法继承父类的原型属性(Animal.prototype)的同时,也在子类的原 ...

  4. 老生常谈--Js继承小结

    一直以来,对Js的继承有所认识,但是认识不全面,没什么深刻印象.于是,经常性的浪费很多时间重新看博文学习继承,今天工作不是特别忙,有幸看到了http://www.slideshare.net/stoy ...

  5. Js继承小结

    Js继承小结 一直以来,对Js的继承有所认识,但是认识不全面,没什么深刻印象.于是,经常性的浪费很多时间重新看博文学习继承,今天工作不是特别忙,有幸看到了http://www.slideshare.n ...

  6. js继承实现

    JS实现继承可以分为:对象冒充和原型链继承 其中对象冒充又包括:临时变量,call 和 apply 临时变量方法: function Person(name,sex){ this.name = nam ...

  7. js继承之借用构造函数继承

    我的上一篇文章介绍了,原型链继承模式.但是单纯的原型链模式并不能很好地实现继承. 一.原型链的缺点 1.1 单纯的原型链继承最大的一个缺点,来自于原型中包含引用类型的值. 本来,我们没有通过原型链实现 ...

  8. js继承之原型链继承

    面向对象编程都会涉及到继承这个概念,JS中实现继承的方式主要是通过原型链的方法. 一.构造函数.原型与实例之间的关系 每创建一个函数,该函数就会自动带有一个 prototype 属性.该属性是个指针, ...

  9. js继承的常用方法

    写在前面的话:这篇博客不适合对面向对象一无所知的人,如果你连_proto_.prototype...都不是很了解的话,建议还是先去了解一下JavaScript面向对象的基础知识,毕竟胖子不是一口吃成的 ...

  10. JS--我发现,原来你是这样的JS:面向对象编程OOP[3]--(JS继承)

    一.面向对象编程(继承) 这篇博客是面向对象编程的第三篇,JS继承.继承顾名思义,就是获取父辈的各种"财产"(属性和方法). 怎么实现继承? 我们的JavaScript比较特别了, ...

随机推荐

  1. 4 Vue.js 核心理念:数据驱动界面

    1 style样式放在body里面 <style type="text/css"> .ui.segment.container { width:700px; } p { ...

  2. Apache 多端口配置方法

    首先修改httpd.conf配置文件. 添加8080端口 Listen 8080 打开虚拟配置文件 # Virtual hosts Include conf/extra/httpd-vhosts.co ...

  3. label标签的作用

    在用户注册的时候,常常用户点击文字就需要将光标聚焦到对应的表单上面,这个是怎么实现的呢?就是下面我要介绍的<label>标签的for属性 定义:for 属性规定 label 与哪个表单元素 ...

  4. interface in iOS

    lo = localhosten = ethernetap = Probably for access point (if you are acting as a wifi host) pdp_ip ...

  5. 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)

    本文导航 -1. 注册并启用红帽订阅 -2. 使用静态 IP 地址配置网络 -3. 设置服务器的主机名称 -4. 更新或升级最小化安装的 CentOS -5. 安装命令行 Web 浏览器 -6. 安装 ...

  6. 《Cracking the Coding Interview》——第17章:普通题——题目7

    2014-04-28 23:28 题目:给定一个数字,用英语把它读出来. 解法:ZOJ上有相反的题目.如果我要用中文读书来呢? 代码: // 17.7 Read an integer in Engli ...

  7. 《Cracking the Coding Interview》——第6章:智力题——题目5

    2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...

  8. Pascal数据结构与算法

    第一章 数据结构与算法的引入 1.1 数据结构的基本概念 一. 学习数据结构的意义 程序设计 = 数据结构 + 算法 目前,80%的待处理的数据具有“算法简单”(四则运算.检索.排序等),“对象复杂” ...

  9. 百度webuploader上传 1

    百度webupload网址:http://fex.baidu.com/webuploader/ 引入js和css <script src="../../Content/webuploa ...

  10. 洛谷P1003铺地毯(提高组)

    题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n. 现在将这些地毯按照编号从小到大的顺序平行于 ...