javascript基础拾遗(九)

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()调用父类构造函数

posted on 2017-12-07 22:04  迪米特  阅读(109)  评论(0编辑  收藏  举报

导航