function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.eating = function() {
console.log("eating~");
}
Person.prototype.runing = function() {
console.log("runing~");
}
function Student(name, age, sno, score) {
// 实现属性继承
Person.call( this, name, age)
this.sno = sno
this.score = score
}
// 实现方法继承
Student.prototype = Object.create(Person.prototype)
// 设置constructor
Object.defineProperty(Student.prototype, "constructor", {
enumerable: false,
configurable: true,
writable: true,
value: Student
})
Student.prototype.studying = function() {
console.log("stying");
}
var stu = new Student("coder", 18, 110, 99)
console.log(stu);
console.log(stu.__proto__);
stu.eating()
stu.runing()
stu.studying()
JavaScript寄生组合实现继承
最新推荐文章于 2024-03-01 16:51:46 发布