美文网首页
JS 中继承的写法

JS 中继承的写法

作者: 饥人谷_刘氏熏肉大饼 | 来源:发表于2018-03-24 20:03 被阅读0次
// 继承
function Human(options){
    this.name = options.name
    this.肤色  = options.肤色
}
Human.prototype.eat = function(){}
Human.prototype.drink = function(){}
Human.prototype.poo = function(){}

function Soldier(options){
    // this.__proto__ = Soldier.prototype//JS 自动加的
    Human.call(this, options)
    this.ID = options.ID
    this.生命值 = 42
}

// 生产环境下要给__ proto___ 赋值, 要使用 Object.create 方法
// Soldier.prototype.__proto__ = Human.prototype
Soldier.prototype = Object.create(Human.prototype)
Soldier.prototype.兵种 = "美国大兵"
Soldier.prototype.攻击力 = 5
Soldier.prototype.行走 = function(){ /*走俩步的代码*/},
Soldier.prototype.奔跑 = function(){ /*狂奔的代码*/  },
Soldier.prototype.死亡 = function(){ /*Go die*/    },
Soldier.prototype.攻击 = function(){ /*糊他熊脸*/   },
Soldier.prototype.防御 = function(){ /*护脸*/       }

var s = new Soldier({name: '方方', 肤色:'yellow', ID: 1})

//上面提到, 我们在生产环境中不能直接使用
Soldier.prototype.__proto__ = Human.prototype
// 那么有什么办法实现这样的功能呢
// 于是我们假设有个构造函数, 也叫做 Human
//它的代码为空, 虽然什么都没写 
//但是在使用 new 的之后, JS 会自动在里面执行三个操作
function Human() {
  //下面都是在 new 时, JS 自动执行的
  this = {}  
  this.__proto__= Human.prototype
  return this
}
// 于是我们可以尝试
Soldier.prototype = new Human()
// 可以得到以下推导
Soldier.prototype === this //this 是 Human 这个构造函数返回的对象
Soldier.prototype.__proto__ === this.__proto__
Soldier.prototype.__proto__ === this.__proto__=== Human.prototype
//这样我们就对__ proto__ 进行了赋值
//但是就像前文说的, 这个空的构造函数不能起名为 Human, 因为已经存在了真正的 Human 构造函数
//于是我们这个这样做, 用一个假的 Human
function fakeHuman(){}
fakeHuman.prototype = Human.prototype
Soldier.prototype = new fakeHuman()
// 这样做就达到了我们的目的, 因为 fakeHuman 的原型就是 Human 的原型, 这个兼容 IE 的写法

// 在 ES6中引入了类似 Java 的 Class 概念
class Human{
    constructor(options){
        this.name = options.name
        this.肤色  = options.肤色
    }
    eat(){}
    drink(){}
    poon(){}
}

class Soldier extends Human{
    constructor(options){
        super(options)
        this.ID = options.ID
        this.生命值 = 42
        this.兵种 = "美国大兵"
        this.攻击力 = 5
    }
    行走(){ /*走俩步的代码*/}
    奔跑(){ /*狂奔的代码*/  }
    死亡(){ /*Go die*/    }
    攻击(){ /*糊他熊脸*/   }
    防御(){ /*护脸*/       }
}
var s = new Soldier({name: '方方', 肤色:'yellow', ID: 1})

下面做一个测试题:

  1. 写出一个构造函数 Animal
  • 输入为空
  • 输出为一个新对象,该对象的共有属性为 {行动: function(){}},没有自有属性
  1. 再写出一个构造函数 Human
  • Human 继承 Animal
  • 输入为一个对象,如 {name: 'Frank', birthday: '2000-10-10'}
  • 输出为一个新对象,该对象自有的属性有 name 和 birthday,共有的属性有物种(人类)、行动和使用工具
  1. 再写出一个构造函数 Asian
  • Asian 继承 Human
  • 输入为一个对象,如 {city: '北京', name: 'Frank', birthday: '2000-10-10' }
  • 输出为一个新对象,改对象自有的属性有 name city 和 bitrhday,共有的属性有物种、行动和使用工具和肤色
function Animal(){}

Animal.prototype.行动 = function(){}

function Human(options) {
  Animal.call(this)
  this.name = options.name
  this.birthday = options.birthday
}
Human.prototype = Object.create(Animal.prototype)
Human.prototype.物种 = '人类'
Human.prototype.使用工具 = function(){}


function Asian(options) {
  Human.call(this, options)
  this.city = options.city
}
Asian.prototype = Object.create(Human.prototype)
Asian.prototype.肤色 = '黄色'

var asian = new Asian({city: '北京', name: 'Frank', birthday: '2000-10-10' })

要求使用 class,完成如下需求:

  1. 写出一个构造函数 Animal
  • 输入为空
  • 输出为一个新对象,该对象的共有属性为 {行动: function(){}},没有自有属性
  1. 再写出一个构造函数 Human
    Human 继承 Animal
  • 输入为一个对象,如 {name: 'Frank', birthday: '2000-10-10'}
  • 输出为一个新对象,该对象自有的属性有 * name、物种和 birthday,共有的属性有行动和使用工具 (由于 class 的语法问题,所以物种只能勉为其难作为一个自有属性,本来应该是共有属性的)
  1. 再写出一个构造函数 Asian
  • Asian 继承 Human
  • 输入为一个对象,如 {city: '北京', name: 'Frank', birthday: '2000-10-10' }
  • 输出为一个新对象,改对象自有的属性有 name city 物种 肤色和 bitrhday,共有的属性有行动和使用工具
class Animal{
  constructor() {

  }
  行动(){}
}

class Human extends Animal{
  constructor(options) {
    super()
    this.name = options.name
    this.birthday = options.birthday
    this.物种 = '人类'
  }
  使用工具() {}
}

class Asian extends Human{
  constructor(options) {
    super(options)
    this.city = options.city
    this.肤色 = '黄色'
  }
}

相关文章

  • class-继承(es6)

    继承-JS 继承-class class-总结 Class 在语法上更加贴合面向对象的写法Class 实现继承更加...

  • JS中继承的写法

    继承是类和类之间的关系,继承使得子类别具有父类别的属性和方法。 js里常用的如下两种继承方式: 原型链继承(对象间...

  • JS中继承的写法

    继承是面向对象编程很重要的一个方面,让子类继承父类的某些属性和方法,是非常常见的需求。 prototype写法 假...

  • JS 中继承的写法

    下面做一个测试题: 写出一个构造函数 Animal 输入为空 输出为一个新对象,该对象的共有属性为 {行动: fu...

  • JS 中继承的写法

    什么是继承 继承(英语:inheritance)是面向对象软件技术当中的一个概念。如果一个类别B“继承自”另一个类...

  • JS中继承的写法

    继承的两种写法 i.Prototype 写法 ii.Class写法 iii.两种方法的区别 两种方法都能实现继承,...

  • JS 中继承的写法

    es5: es6: class 在原型上声明一个不是函数的属性:es5:Human.prototype.race ...

  • js组件化开发

    如果不了解js的继承的写法,可以先去看看我之前写的js的子类继承父类文章http://www.jianshu.co...

  • onclick JS的错误写法

    如图报了not defined 的错误 我的错误写法 js中 function a(){} 正确写法 js中 a=...

  • JS 的面向对象

    JS 不是一门面向对象的语言,但是很多情况我们需要面向对象。 一、JS 继承的常用写法。 为什么一上来就写常用写法...

网友评论

      本文标题:JS 中继承的写法

      本文链接:https://www.haomeiwen.com/subject/iehccftx.html