1.class关键字
ES6引入了新的class关键字编写对象

function Language(name){
        this.name = name
        this.score = 8.0
    }
    Language.prototype.popular = function () {
        return this.score/10*100 + '%'
    }

    class Language{
        constructor(name){
            this.name = name
            this.score = 8.0
        }
        hello() {
            return this.score/10*100 + '%'
        }
    }

新的class关键字,避免了属性和原型方法分散的情况

2)class继承

class Language{
        constructor(name){
            this.name = name
            this.score = 8.0
        }
        hello() {
            return this.score/10*100 + '%'
        }
    }
    class FastLanguage extends Language{
        constructor(name, speed){
            super(name)
            this.speed = speed
        }
    }
    language = new FastLanguage('python',0.01)
    console.log(language.speed)
    console.log(language.name)

extends:继承
super()调用父类构造函数

相关文章:

  • 2021-07-01
  • 2021-09-27
  • 2022-01-25
  • 2021-10-10
  • 2021-10-26
  • 2021-07-27
  • 2022-01-14
  • 2021-06-26
猜你喜欢
  • 2021-12-04
  • 2022-01-03
  • 2022-01-13
  • 2021-12-20
  • 2021-07-15
  • 2021-09-02
  • 2021-09-03
相关资源
相似解决方案