首页 > 其他 > 详细

ES6(十二)类与对象

时间:2019-09-21 19:40:24      阅读:104      评论:0      收藏:0      [点我收藏+]
构造函数和实例

class Person {
  constructor (name, age) {
    this.name = name
    this.age = age
  }
}

let vPerson = new Person(‘v‘, 10)
console.log(‘构造函数和实例‘, vPerson)

继承

class Child extends Person {
}

console.log(‘继承‘, new Child(‘kaka‘, 10))

继承传递参数

class Child extends Person {
  constructor (name = ‘child‘) {
    super(name)
    this.type = ‘game‘
  }
}

console.log(‘继承传递参数‘, new Child(‘hello‘))

getter  setter

class Person {
  constructor (name = ‘kaka‘, age) {
    this.name = name
    this.age = age
  }

  get longName () {
    return ‘hello ‘ + this.name
  }

  set longName (value) {
    this.name = value
  }
}

let v = new Person()
console.log(‘getter‘, v.longName)
v.longName = ‘ronle‘
console.log(‘setter‘, v.longName)

静态方法

class Person {
  constructor (name = ‘kaka‘) {
    this.name = name
  }

  static tell () {
    console.log(‘tell‘)
  }
}
Person.tell()

静态属性

class Person {
  constructor (name = ‘kaka‘) {
    this.name = name
  }
}
Person.type = ‘game‘
console.log(‘静态属性‘, Person.type)

 

ES6(十二)类与对象

原文:https://www.cnblogs.com/ronle/p/11563991.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!