是否存在对es6类的多级继承的限制。我正在向框架/外部类添加一些额外的功能。
class B extends External.A {
// ...
}
class C extends B {
// ...
}并使用类似的
const result = new C(data);给出误差
TypeError:没有“新”类构造函数B就无法调用
但是,如果我直接使用类A,则不会出现错误。
class C extends External.A {
// ...
}
const result = new C(data);//工作良好
编辑:我使用"babel": "^6.5.2"来传输所有内容。在实际代码中,所有三个类都位于不同的文件中,并使用模块系统来导出和导入它们。如果有关系的话。
发布于 2016-05-22 13:08:25
原始答案
你的问题的基本原理就像魅力一样。可能您的代码缺少super()中的constructor()。
class A {
constructor() {
document.write('from A <br/>');
}
}
class B extends A {
constructor() {
super();
document.write('from B <br/>');
}
}
class C extends B {
constructor() {
super();
document.write('from C <br/>');
}
}
new C();这里是你可以玩的小提琴:https://jsfiddle.net/nkqkthz2/
编辑
在您最初的问题中,您将一些数据传递给C类构造函数new C(data);,然后如果您想在类链中处理这个问题,您应该编写自己的构造函数:
class A {
constructor(data) {
document.write(`${data} A <br/>`);
}
}
class B extends A {
someFunc() {
//
}
}
class C extends B {
constructor(data) {
super(data);
this.data = data;
}
write() {
document.write(`${this.data} C <br/>`);
}
}
const c = new C('test');
c.write();https://jsfiddle.net/rwqgm9n0/
注意B类不需要指定构造函数,因为默认构造函数是:
constructor(...args) {
super(...args);
}这可以将数据传递给A类构造函数。
如果在C类构造函数中省略了超级函数,那么更新的数据将传递给A类,这会产生错误。
class A {
constructor(data) {
document.write(`${data} A <br/>`);
}
}
class B extends A {
someFunc() {
//
}
}
class C extends B {
constructor(data) {
//super(data);
this.data = data;
}
write() {
document.write(`${this.data} C <br/>`);
}
}
const c = new C('test');
c.write();发布于 2017-02-27 16:40:09
多级继承:
class Person{
constructor(firstName, middleName, lastName){
console.log("Person constructor........");
this.firstName=firstName;
this.middleName=middleName;
this.lastName=lastName;
}
fullName() {
return `${this.firstName}${this.middleName==null?' ':' '+this.middleName+' '}${this.lastName}`;
}
}
//let person = new Person("FirstName",null, "LastName");
//console.log('Full Name: '+person.fullName());
// INHERITANCE
class Employee extends Person{
constructor(employeeId, joinDate,firstName, middleName, lastName){
super(firstName, middleName, lastName);
console.log("Employee constructor........");
this.employeeId = employeeId;
this.joinDate = joinDate;
}
}
/*let employee = new Employee(12, '2017-02-01', "FirstName",null, "LastName");
console.log('Full Name: '+employee.fullName());
console.log('Employee ID: '+employee.employeeId);
console.log('Join Date: '+employee.joinDate);*/
class EmpOne extends Employee{
constructor(empOneId,employeeId, joinDate,firstName, middleName, lastName){
super(employeeId, joinDate,firstName, middleName, lastName);
console.log("EmpOne constructor.........");
this.empOneId = empOneId;
}
}
let newEmpOne = new EmpOne("emp one ID", 13, '2018-02-01', "FirstName",null, "LastName");
console.log("==================================")
console.log('Full Name: '+newEmpOne.fullName());
console.log('Employee ID: '+newEmpOne.employeeId);
console.log('Join Date: '+newEmpOne.joinDate);
console.log('EMP ONE: '+newEmpOne.empOneId);
https://stackoverflow.com/questions/37374317
复制相似问题