es6类和多级继承

时间:2016-05-22 12:21:49

标签: javascript ecmascript-6

是否对es6类的多级继承有限制。我正在为框架/外部类添加一些额外的功能。

Type mismatch

并使用

class B extends External.A {
  // ...
}

class C extends B {
  // ...
}

给出错误

  

TypeError:如果没有' new'

,则无法调用类构造函数B.

但是,如果我直接使用类const result = new C(data); ,则没有错误

A

//工作正常

编辑: 我用class C extends External.A { // ... } const result = new C(data); 来描述一切。在实际代码中,所有3个类都存在于不同的文件中,并使用模块系统导出和导入它们。如果重要的话。

2 个答案:

答案 0 :(得分:0)

原始答案

你问题的骨头就像魅力一样。 可能您的代码在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类构造函数中省略super,则表示您将数据传递给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();

https://jsfiddle.net/jwm5xjcp/

答案 1 :(得分:0)

多级继承:

&#13;
&#13;
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);
&#13;
&#13;
&#13;