1、属性继承 :call 、apply:不建议使用浪费内存。

function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
} Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
} function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex) this.larynx = larynx;
this.beard = beard; }
Man.prototype.work = function(){
console.log('111')
}
var songlei = new Man(10,20);
console.log(songlei); // Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// }

2、原型继承:

  缺点:原型继承会污染父级
function Person(name,age,sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
}
Person.prototype.eat = function(){
    console.log("正在吃饭")
}
Person.prototype.sleep = function(){
    console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
    Person.call(this,name,age,sex)
    this.larynx = larynx;
    this.beard = beard;
    
}
Man.prototype = Person.prototype;
Man.prototype.work = function(){
    console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
    // Man{
        // age: undefined
        //  beard: 20
        //  larynx: 10
        //  name: undefined
        //  sex: undefined
        //  __proto__:
            //  eat: ƒ()
            //  sleep: ƒ()
            //  work: ƒ()
            //  constructor: ƒ Person(name, age, sex)
    // }
var p1 = new Person()
console.log(p1)
    // Person{
        // age: undefined
        // name: undefined
        // sex: undefined
        // __proto__:
        // eat: ƒ()
        // sleep: ƒ()
        // work: ƒ()
        // constructor: ƒ Person(name, age, sex)
    // }

3、原型拷贝:

function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard; } for(var key in Person.prototype){
Man.prototype[key] = Person.prototype[key]
}
Man.prototype.work = function(){
console.log('111')
} var aaa = new Man(10,20);
console.log(aaa);
// Man { name: undefined, age: undefined, sex: undefined, larynx: 10, beard: 20 }
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// work: ƒ()
// constructor: ƒ Man(larynx, beard, name, age, sex)
var p1 = new Person()
console.log(p1)
// Person { name: undefined, age: undefined, sex: undefined }
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// constructor: ƒ Person(name, age, sex)

4、原型链继承:

原型链:
        由__proto__组成的链条叫做原型链
  原型链继承是不推荐使用的
        因为会多了好多无用的属性
        而且还少了constructor
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
} Person.prototype.eat = function(){
console.log("正在吃饭")
} Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex) this.larynx = larynx;
this.beard = beard; }
// __proto //__proto__
Man.prototype = new Person();
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// __proto__: Person
// age: undefined
// name: undefined
// sex: undefined
// work: ƒ()
// } var p1 = new Person()
console.log(p1)
// Person{
// age: undefined
// name: undefined
// sex: undefined
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// constructor: ƒ Person(name, age, sex)
// }

5、寄生继承:

缺点:增加了无用的函数

function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.type="人类";
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard; }
//创建了一个寄生器
function fn(){};
//寄生器的原型对象 = 人类的原型对象
fn.prototype = Person.prototype;
//原型链继承 寄生器的实例对象
Man.prototype = new fn();
Man.prototype.constructor = Man;
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// __proto__: Person
// constructor: ƒ Man(larynx, beard, name, age, sex)
// work: ƒ()
// }

6、混合继承:我最喜欢的一种方式

function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.type="人类";
Person.prototype.eat = function(){
console.log("正在吃饭")
}
Person.prototype.sleep = function(){
console.log("正在睡觉")
}
function Man(larynx,beard,name,age,sex,){
Person.call(this,name,age,sex)
this.larynx = larynx;
this.beard = beard;
}
//Man.prototype = Object.create(Person.prototype);
Man.prototype = {
constructor:Man,
__proto__:Person.prototype
}
Man.prototype.work = function(){
console.log('111')
}
var aaa = new Man(10,20);
console.log(aaa);
// Man{
// age: undefined
// beard: 20
// larynx: 10
// name: undefined
// sex: undefined
// __proto__: Person
// constructor: ƒ Man(larynx, beard, name, age, sex)
// work: ƒ()
// } var p1 = new Person();
console.log(p1)
// Person{
// age: undefined
// name: undefined
// sex: undefined
// __proto__:
// eat: ƒ()
// sleep: ƒ()
// type: "人类"
// constructor: ƒ Person(name, age, sex)
// }

7、Es6继承

ES6类的语法
            1、声明类的时候用 class
       class 类名{
            constructor(){
                属性
            }
            方法
        }
class Person{
constructor(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
eat(){}
sleep(){}
}
class Man extends Person{
constructor(larynx,beard){
//实现继承必须使用super
super();
this.larynx = larynx;
this.beard = beard;
}
work(){}
}
var aaa = new Man()
console.log(aaa)
// Man{
// age: undefined
// beard: undefined
// larynx: undefined
// name: undefined
// sex: undefined
// __proto__: Person
// constructor: class Man
// work: ƒ work()
// }

js继承的几种方法理解和代码演示的更多相关文章

  1. 实现JS继承的几种方法

    总的来说,JS的继承大体上分为两种:借用构造函数方式和原型方式 首先,我们来看看借用构造函数方式的几种做法: //方式一function Person(name, sex){ this.name = ...

  2. JS继承的6种方法

    1.原型链 基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法. 构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原 ...

  3. JS学习笔记——JavaScript继承的6种方法(原型链、借用构造函数、组合、原型式、寄生式、寄生组合式)

    JavaScript继承的6种方法 1,原型链继承 2,借用构造函数继承 3,组合继承(原型+借用构造) 4,原型式继承 5,寄生式继承 6,寄生组合式继承 1.原型链继承. <script t ...

  4. js对象之间的"继承"的五种方法

    今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...

  5. 4.Javascript中实现继承的几种方法及其优缺点

    要搞懂JS继承,我们首先要理解原型链:每一个实例对象都有一个__proto__属性(隐式原型),在js内部用来查找原型链:每一个构造函数都有prototype属性(显示原型),用来显示修改对象的原型, ...

  6. js去除空格12种方法

    注:本文非本人原著:原文作者: 黄卉  <js去除空格12种方法> //JS去除空格的方法目前共有12种: //实现1 String.prototype.trim = function() ...

  7. 判断数组的方法/判断JS数据类型的四种方法

    参考文: 以下 3 个判断数组的方法,请分别介绍它们之间的区别和优劣Object.prototype.toString.call() . instanceof 以及 Array.isArray() h ...

  8. Java执行shell脚本并返回结果两种方法的完整代码

    Java执行shell脚本并返回结果两种方法的完整代码 简单的是直接传入String字符串,这种不能执行echo 或者需要调用其他进程的命令(比如调用postfix发送邮件命令就不起作用) 执行复杂的 ...

  9. js继承的几种实现方法

    一.用function实现: function Person(name) { this.name = name; } Person.prototype.getName = function() { r ...

随机推荐

  1. janusgraph-控制台操作命令

    当顶点数量过多时(我的230w)删除太慢 就用下面的命令, 删除整个图库 graph.close() JanusGraphFactory.drop(graph) 查询所有的顶点属性 用traversa ...

  2. ztree异步加载---------补发周日内容

    上周六老师要求和大三的进行JAVA知识交流,总体来说就是给大三学长做的东西打分,然后大三学长再教我们如果构建ztree.毕竟第一次接触ztree,所以有很多不了解,但通过周六日努力,还是做出来了.现在 ...

  3. 27-ESP8266 SDK开发基础入门篇--编写Android SmartConfig一键配网程序

    style="font-size: 18pt;">https://www.cnblogs.com/yangfengwu/p/11429007.html https://wik ...

  4. 产品生命周期(Product Life Circle,PLC)

    什么是产品生命周期? 产品生命周期是新产品从开发进入市场到被市场淘汰的整个过程.产品生命周期可分为初创期.成长期.成熟期.衰退期. 产品生命周期有什么用? 在产品不同的生命阶段,公司的业务目的都不同. ...

  5. 区间DP训练

    一.石子合并 问题描述 将 n (\(1 \le n \le 200\))堆石子绕圆形操场摆放,现要将石子有次序地合并成一堆.规定每次只能选相邻的两堆合并成新的一堆,并将新的一堆的石子数,记为该次合并 ...

  6. mysql 显示表结构

    例子 mysql> show columns from table1; +------------+------------------+------+-----+---------+----- ...

  7. Java中定义不了可变长数组怎么办---集合 泛型

    一.集合(Collections) Java使用集合来组织和管理对象. 1.Java的集合类 集合类主要负责保存.盛装和管理对象,因此集合类也被称为容器类. 集合类分为Set.List.Map和Que ...

  8. MATLAB关闭科学计数法显示

  9. Linux 磁盘的分区

    如果我们想在系统中新增一块硬盘,需要做什么呢? 1. 对磁盘进行分区,新建可用分区 2. 对该分区进行格式化,以创建系统可用的文件系统 3. 若想要仔细一点,可以对刚才新建好的文件系统进行检验 4. ...

  10. Linux系统学习(二)一Linux基本操作

    一.Linux的目录结构 1.1 Linux的目录结构图 1.2 目录内容 /:这就是根目录.对你的电脑来说,有且只有一个根目录.所有的东西,我是说所有的东西都是从这里开始.举个例子:当你在终端里输入 ...