首页 > 试题广场 >

function Parent(name) { this.n

[单选题]
function Parent(name) {
  this.name = name;
}
Parent.prototype.sayHello = function() {
  return 'Hello, '+ this.name;
};
function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
var child = new Child('Alice', 10);
console.log(child.sayHello());
上面这段代码打印的结果是什么()
  • 'Hello, Alice'
  • undefined
  • Error
  • 'Hello, undefined'
组合继承:
function Parent(name) {
  this.name = name;
}
Parent.prototype.sayHello = function () {
  return 'Hello ' + this.name;
};

function Child(name, age) {
  Parent.call(this, name); // 继承实例属性
  this.age = age;
}

// 继承原型方法
Child.prototype = Object.create(Parent.prototype);
// 修正 constructor
Child.prototype.constructor = Child;


发表于 2026-03-29 12:42:15 回复(0)