小编Gon*_*ack的帖子

Javascript函数中的继承错误

我以下列方式在JavaScript中应用继承:

var employee = function(name) {
  this.name = name;
}
employee.prototype.getName = function() {
  return this.name;
}
var pEmployee = function(salary) {
  this.salary = salary;
}
pEmployee.prototype.getSalary = function() {
  return this.salary;
}

var employee = new employee("mark");
pEmployee.prototype = employee;
var pe = new pEmployee(5000);
console.log(pe.getName());
console.log(pe.getSalary());
Run Code Online (Sandbox Code Playgroud)

但它在控制台中显示以下错误:

未捕获的TypeError:pe.getSalary不是函数

谁能告诉我这个错误背后的原因是什么?

javascript inheritance

2
推荐指数
1
解决办法
41
查看次数

标签 统计

inheritance ×1

javascript ×1