function create(subObj,parentObj){
const newObj = Object.create(parentObj)
subObj.prototype = newObj
Object.defineProperty(subObj.prototype,'constructor',{
configurable:false,
enumerable:false,
writable:true,
value:subObj,
})
}
function createObj1(subObj,parentObj){
const a = {}
Object.setPrototypeOf(a,parentObj.prototype)
Object.defineProperty(a,'constructor',{
configurable:false,
enumerable:false,
writable:true,
value:subObj,
})
subObj.prototype = a
}
function createObj(subObj,parentObj){
function a(){}
a.prototype = parentObj.prototype
const newObj = new a()
Object.defineProperty(newObj,'constructor',{
configurable:false,
enumerable:false,
value:subObj,
writable:true,
})
return newObj
}
function Person(name,age){
this.name = name
this.age = age
}
Person.prototype.eating = () => {
console.log("eating")
}
function Student(name,age,sno) {
Person.call(this,name,age)
this.sno = sno
}
//方式一
// create(Student,Person.prototype)
//方式二
// Student.prototype = createObj(Student,Person)
//方式三
createObj1(Student,Person)
Student.prototype.study = () => {
console.log("study")
}
const stu = new Student("zhangsan",18,'0001')
console.log(stu.name,stu.age,stu.sno)
stu.eating()
console.log(stu)
//上面三个方法中的Object.defineProperty就是为了修改此处打印结果的
console.log(stu.constructor.name)
console