Uncaught TypeError: Cannot set property 'getName' of undefined(anonymous function)

Uncaught TypeError: Cannot set property 'getName' of undefined(anonymous function)

 <script>  
    var Employee = new function(name)
    {
     this.name=name;
    }
    Employee.prototype.getName = function()
    {
      return this.name;
    }

    var PermanenetEmployee = new function(annualsalary)
    {
    this.annualsalary=annualsalary;
    }
    var employee = new Employee("rahul");
    PermanenetEmployee.prototype = employee;
    var pe = new PermanenetEmployee(5001);
    document.write(pe.getName());



    </script> 

我正在 java 脚本中实现继承。从这段代码中,我想打印员工姓名,如 "rahul"。但是我在这方面遇到错误,如 Uncaught TypeError: Cannot set 属性 'getName' of undefined(anonymous function)。如何解决这个问题错误?

Employee.prototype.getName = function()
        {
          return this.name;
        }

这是问题所在:

var Employee = new function(name)
// ------------^^^
{
 this.name=name;
}

PermanenetEmployee也是如此。)

你不想 new 在那里。 new 调用函数。您想稍后再做,就像您在分配给 employee.

时所做的那样

请注意,您在它们之间设置继承的方式是一种反模式。要使 PermanenetEmployee 正确 "subclass" Employee,请执行以下操作:

PermanenetEmployee.prototype = Object.create(Employee.prototype);
PermanenetEmployee.prototype.constructor = PermanenetEmployee;

没有

var employee = new Employee("rahul");
PermanenetEmployee.prototype = employee;

...然后让 PermanenetEmployee 接受 name 并将其传递给 Employee:

var PermanenetEmployee = function(name, annualsalary) {
    Employee.all(this, name); // <====
    // ...
};

...或更好地使用,使用 ES2015 ("ES6") class(如果需要,例如使用 Babel 进行转译)。

这是一个正确的设置。我还修复了 PermanenetEmployee:

中的错字

var Employee = function(name) {
    this.name = name;
};
Employee.prototype.getName = function() {
    return this.name;
};

var PermanentEmployee = function(name, annualSalary) {
    Employee.call(this, name);
    this.annualSalary = annualSalary;
};

// Set up subclass
PermanentEmployee.prototype = Object.create(Employee.prototype);
PermanentEmployee.prototype.constructor = PermanentEmployee.prototype;

PermanentEmployee.prototype.getAnnualSalary = function() {
    return this.annualSalary;
};

// Using
var pe = new PermanentEmployee("Rahul", 5001);
console.log(pe.getName());
console.log(pe.getAnnualSalary());

以及 ES2015:

class Employee {
    constructor(name) {
        this.name = name;
    }
    getName() {
        return this.name;
    }
}

class PermanentEmployee extends Employee {
    constructor(name, annualSalary) {
        super(name);
        this.annualSalary = annualSalary;
    }

    getAnnualSalary() {
        return this.annualSalary;
    }
}

// Using
var pe = new PermanentEmployee("Rahul", 5001);
console.log(pe.getName());
console.log(pe.getAnnualSalary());

再次注意,如果您想在野外使用该语法(目前),则需要进行转译。

有几种方法可以让继承在 JS 中工作,我正在使用这种模式。

首先声明基础原型:

Employee = function () {
};
Employee.prototype = {
    getName: function () {}
};

然后是继承base的原型:

PermanentEmployee = function () {
    Employee.call(this);
};

PermanentEmployee.prototype = Object.create(Employee.prototype);
PermanentEmployee.constructor = PermanentEmployee;

PermanentEmployee.prototype.foo = function() {}