JavaScript学习-类/原型链/class

服了,这个原型链是个什么???一直以来C/C++/java类的概念都深入我心了,突然搞这个很不适应。有啥用啊?

而且看到后面ES6语法,竟然也加入了class????

搞什么呢?这不是打自己脸吗?

或许后面会有很有用的地方,但是暂时没有发现。

关于原型链的讲解

https://blog.csdn.net/m0_37589327/article/details/78655038

类继承是通过原型链的,而class,就是一个封装了原型链的API

class能让我们像Java一样写代码

现在先用class好了,原型链了解一下,毕竟不是深入学习js

class Student {


   //构造函数    
    constructor(name) {
        this.name = name;
    }

   //方法
    hello() {
        alert('Hello, ' + this.name + '!');
    }
}

var xiaoming = new Student('小明');
xiaoming.hello();
 

继承

class PrimaryStudent extends Student {
    constructor(name, grade) {
        super(name); // 记得用super调用父类的构造方法!
        this.grade = grade;
    }

    myGrade() {
        alert('I am at grade ' + this.grade);
    }
原文地址:https://www.cnblogs.com/weizhibin1996/p/9278325.html