美文网首页
3. 声明&&属性&&方法&&继承

3. 声明&&属性&&方法&&继承

作者: yaoyao妖妖 | 来源:发表于2020-10-13 17:35 被阅读0次

ES5 如何声明一个类

let Animal = function (type) {
     // 属性
  this.type = type
}
// 原型链上挂载方法做继承
Animal.prototype.eat = function () {
  console.log('i am eat food hello')
}

let dog = new Animal('dog')
let monkey = new Animal('monkey')

console.log(dog)
console.log(monkey)
// 修改原型链上继承的方法
monkey.constructor.prototype.eat = function () {
  console.log('error')
}

dog.eat()
monkey.eat()

ES6 如何声明一个类,class只是语法糖,本质和ES5的声明类是一致的

class Animal {
  constructor (type) {
    this.type = type
  }
  eat () {
    console.log('i am eat food')
  }
}

let dog = new Animal('dog')
let monkey = new Animal('monkey')
console.log(dog)
console.log(monkey)
dog.eat()
monkey.eat()
// 判断类型    function
console.log(typeof Animal)

属性 私有属性 只读

let _age = 4
class Animal {
  constructor (type) {
    this.type = type
  }
    属性的读写和属性的保护(只读)
  get age () {
    return _age
  }
  set age (val) {
    if (val < 7 && val > 4) {
      _age = val
    }
  }
  eat () {
    console.log('i am eat food')
  }
}
let dog = new Animal('dog')
console.log(dog.age)
dog.age = 8
console.log(dog.age)
console.log(dog._age)

ES5 类的静态方法和实例对象方法

let Animal = function (type) {
  this.type = type
}
// 实例对象方法
Animal.prototype.eat = function () {
  Animal.walk()
  console.log('i am eat food hello')
}
// 类的静态方法
Animal.walk = function () {
  console.log('i am walking')
}
let dog = new Animal('dog')
dog.eat()

ES6 类的静态方法和实例对象方法

class Animal {
  constructor (type) {
    this.type = type
  }
  eat () {
    Animal.walk()
    console.log('i am eat food')
  }
  static walk () {
    console.log('i am walking')
  }
}
let dog = new Animal('dog')
dog.eat()
let Animal = function (type) {
  this.type = type
}

// ES5 中继承

Animal.prototype.eat = function () {
  Animal.walk()
  console.log('i am eat food hello')
}

Animal.walk = function () {
  console.log('i am walking')
}

let Dog = function () {
  // 初始化父类的构造函数,改变this的指向,animal的this指向dog的实例
  Animal.call(this, 'dog')
  this.run = function () {
    console.log('i can run')
  }
}
// 值类型,引用类型
Dog.prototype = Animal.prototype

let dog = new Dog('dog')
dog.eat()

ES6 中继承

class Animal {
  constructor (type) {
    this.type = type
  }
  eat () {
    Animal.walk()
    console.log('i am eat food')
  }
  static walk () {
    console.log('i am walking')
  }
}

class Dog extends Animal {
  // 显式,隐式
  constructor (type) {
    super(type)
    this.age = 2
  }
}

let dog = new Dog('dog')
dog.eat()

学习视频记录

相关文章

  • 3. 声明&&属性&&方法&&继承

    ES5 如何声明一个类 ES6 如何声明一个类,class只是语法糖,本质和ES5的声明类是一致的 属性 私有属性...

  • Dart笔记(四)面向对象基本特性

    1.class关键字声明2.使用new 创建对象,new可省略3.所有对象都继承自Object类 属性和方法 属性...

  • 添加对象属性

    添加对象属性: 对象属性其实是通过继承init方法继承下来的 练习: 声明人类有属性:名字、年龄、性别声明学生类有...

  • 添加对象属性

    添加对象属性:对象属性其实是通过继承init方法继承下来的 练习: 声明人类有属性:名字、年龄、性别 声明学生类有...

  • Java 的小白学习笔记九(面向对象-继承)

    继承 继承特性 1. 子类拥有父类非private的属性,方法 2. 子类可以拥有自己的属性和方法 3. 子类可以...

  • javascript 类的私有/公有/静态属性、方法、特权方法

    1. ES5的声明方法 2. ES6的声明方法 3. 调用 私有属性 公共属性 私有方法 特权方法: 公共方法 原...

  • JS 继承

    1.原型链 查找属性、方法的路径 通过修改原型对象实现继承 2.组合继承 通过借调使属性共享继承 3.借调 1.c...

  • Android 自定义控件-自定义进度条。

    效果图: 1.编写类继承View 2.重写构造方法 3.自定义属性 4.声明需要的变量以及常量 5.测量View大...

  • Swift2.0 代理的使用

    标签(空格分隔): IOS-Swift [toc] 声明一个代理协议 声明代理属性 调用代理方法 给代理赋值 继承...

  • ES6(四)—— Class

    目录 Class类的使用【声明、属性、方法、继承】Basic Syntax —— 怎么声明一个类?ES5ES6类和...

网友评论

      本文标题:3. 声明&&属性&&方法&&继承

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