【前端】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 {}

posted @ 2017-01-04 15:27  赵康  阅读(853)  评论(0编辑  收藏  举报