利用js的call实现属性的继承

/**
 * 学生类,代表一个学生.
 * @constructor
 * @param {string} name - 学生的姓名.
 * @param {string} id - 学生的学号.
 */
function Student(name, id) {
    this.name = name;
    this.id = id;
}

/**
 * 小学生类,代表一个小学生.
 * @constructor
 * @param {string} name - 小学生的姓名.
 * @param {string} id - 小学生的学号.
 */
function Pupils(name, id) {     
    Student.call(this, name, id);  // 利用call调用Student(父类)构造函数,继承Student属性
    this.grade = 'primary school'; // 年级
}

console.log(new Pupils('小明', '3000000001').name); // 小明

你可能感兴趣的:(frontend)