一、对象冒充

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

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

 

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

四、原型链方式

即子类通过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);
}
 Child.prototype.constructor=Child; //这行的作用是:默认指向Person,需要手动更改为Child,每个对象也有constructor
var c = new Child();
c.sayHello();
c.sayWorld();

五、混合方式
      混合了call方式、原型链方式

        最佳做法:成员属性继承使用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);
}
  Child.prototype.constructor=Child; //这行的作用是:默认指向Person,需要手动更改为Child,每个对象也有constructor
var c = new Child("jamesbing","gg");
c.sayHello();
c.sayWorld();

本文转自:http://blog.csdn.net/james521314/article/details/8645815

JavaScript 五种(构造方式)继承的更多相关文章

  1. iOS开发中数组常用的五种遍历方式

    随着iOS的不断发展,apple也不断推出性能更高的数组遍历方式,下面将对熟悉的五种遍历方式进行列举. 首先定义一个数组,并获取数组长度 NSArray *array=@[",]; NSIn ...

  2. Spring事务Transaction配置的五种注入方式详解

    Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学 ...

  3. CacheConcurrencyStrategy五种缓存方式

    CacheConcurrencyStrategy有五种缓存方式:  CacheConcurrencyStrategy.NONE,不适用,默认  CacheConcurrencyStrategy.REA ...

  4. IPC五种通讯方式

    IPC五种通讯方式 1.管道:速度慢,容量有限,只有父子进程能通讯 2.FIFO:任何进程间都能通讯,但速度慢 3.消息队列:容量受到系统限制,且要注意第一次读的时候,要考虑上一次没有读完数据的问题 ...

  5. LFU五种实现方式,从简单到复杂

    前言 最近刷力扣题,对于我这种 0 基础来说,真的是脑壳疼啊.这个月我估计都是中等和困难题,没有简单题了. 幸好,力扣上有各种大牛给写题解.看着他们行云流水的代码,真的是羡慕不已.让我印象最深刻的就是 ...

  6. JavaScript五种继承方式详解

    本文抄袭仅供学习http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html 一. 构造函数绑定 ...

  7. javascript类继承系列五(其他方式继承)

    除了前面学习的三种继承外,还有另外三种:原型继承寄生继承,寄生组合继承都是以: function object(o) { function F() { } F.prototype = o; retur ...

  8. JS 跨域问题常见的五种解决方式

    一.什么是跨域? 要理解跨域问题,就先理解好概念.跨域问题是由于javascript语言安全限制中的同源策略造成的. 简单来说,同源策略是指一段脚本只能读取来自同一来源的窗口和文档的属性,这里的同一来 ...

  9. Flex页面跳转的五种实现方式

    Flex页面跳转有很多值得学习的地方,本文向大家介绍一下Flex页面跳转的几种方式,主要包括五种方式,这里为大家一一介绍. AD:   在学习Flex的过程中,你可能会遇到Flex页面跳转的概念,这里 ...

随机推荐

  1. perl array, scalar and hash

    #!/usr/bin/perl use strict; use warnings; my @aa=("aa", "bb", "cc"); p ...

  2. 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)

    4692: Beautiful Spacing Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 46  Solved: 21[Submit][Statu ...

  3. NuGet包引用依赖问题

    如A包需要引用B包,然后在项目中安装A包时,自动把B包也装上. 在nuget命令行打包的时候,如果是指定项目的csproj文件进行打包的,都只能打包当前项目的dll,那么如果使用了第三方的dll而没有 ...

  4. List排序和去重

    //去重和排序 List<SysResource> sortList = new ArrayList<SysResource>(); sortList.addAll(list) ...

  5. Server 对象

    如何解决Response.Redirect方法传递汉字丢失或乱码问题? 为了确保传递的汉字被正确地接收,可以在传值之前使用Server对象的UrlEncode方法对所传递的汉字进行URL编码.代码如下 ...

  6. Linux下动态库查找路径的问题

    说到和动态库查找路径相关的问题,总体上可以分为两类:    第一类: 通过源代码编译程序时出现的找不到某个依赖包的问题,而如果此时你恰好已经按照它的要求确确实实.千真万确.天地良心地把依赖库给装好了, ...

  7. FZU 1752 A^B mod C(快速加、快速幂)

    题目链接: 传送门 A^B mod C Time Limit: 1000MS     Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...

  8. CBOW Model Formula Deduction

    Paper Reference: word2vec Parameter Learning Explained 1. One-word context Model In our setting, the ...

  9. 我总结的js方面你可能不是特别清楚的小知识

    !!将一个值方便快速转化为布尔值 console.log( !!window===true ); 不声明第三个变量实现交换 var a=1,b=2; a=[b,b=a][0];//执行完这句代码之后 ...

  10. TCP/IP --- UDP Broadcast Address

    Related information link : 百度百科---->广播地址 Use restrictions: 1. You can only broadcast on the same ...