K0II/JavaScript

ES6 class

Opened this issue · 0 comments

K0II commented
class student {
    constructor(name) {
        this.name = name;
    }

     //  定义在原型对象上的函数hello()
    hello() {                 
        alert(this.name);
    }
}

var foo = new student('xiaoming');
foo.hello();

// 继承
class primaryStudent extends student {
    constructor(name,grade) {
        super(name);    //  用super调用父类的构造方法
        this.grade = grade;
    }

    myGrade() {
        alert(this.grade);
    }
}

var bar = new primaryStudent('John', 3);
bar.hello();
bar.myGrade();

我觉得ES6的class真的挺鸡肋的,就是换了种写法嘛,能不能别学O-O语言的class了,我觉得prototype理解了就很好用啊,本质上就不同的东西何必搞来搞去越来越和别人像,不知道js为什么总要一副要迁就谁的样子?