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

2023-10-17 09:54:38 浏览数 (1)

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

代码语言:javascript复制
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 {}

0 人点赞