zoukankan      html  css  js  c++  java
  • ES6(十二)类与对象

    构造函数和实例
    
    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)
  • 相关阅读:
    160-13. 罗马数字转整数
    159-118. 杨辉三角
    158-190. 颠倒二进制位
    157-461. 汉明距离
    156-412. Fizz Buzz
    155-278. 第一个错误的版本
    154-108. 将有序数组转换为二叉搜索树
    153-101. 对称二叉树
    152-234. 回文链表
    秒杀程序架构演进
  • 原文地址:https://www.cnblogs.com/ronle/p/11563991.html
Copyright © 2011-2022 走看看