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

版权声明:可任意转载,转载注明出处。 https://blog.csdn.net/Handsome_fan/article/details/80781866
/**
 * 学生类,代表一个学生.
 * @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); // 小明

猜你喜欢

转载自blog.csdn.net/Handsome_fan/article/details/80781866