【前端】js中new和Object.create()的区别

时间:2023-03-08 21:59:05
【前端】js中new和Object.create()的区别

js中new和Object.create()的区别

var Parent = function (id) {
this.id = id
this.classname = 'Parent'
} Parent.prototype.getId = function() {
console.log('id:', this.id)
}; var Child = function (name) {
this.name = name
this.classname = 'Child'
} Child.prototype.getName = function() {
console.log('name:', this.name)
}; var p1 = new Parent(1)
var p2 = Object.create(Parent.prototype) console.log(p1)
// Parent {id: 1, classname: "Parent"} console.log(p2)
// Parent {}