我如何在 ES6 类 中使用方法组合?
How can I use composition with methods in ES6 Classes?
我正在努力在 ES6 中实现组合 classes!我试图同时理解很多事情,可能犯了一个愚蠢的错误。
我想暂时避免使用 extend
和 super
的继承,并且找到了这个很好的组合示例,它似乎展示了我所追求的:
class EmployeeTaxData {
constructor(ssn, salary) {
this.ssn = ssn;
this.salary = salary;
}
// ...
}
class Employee {
constructor(name, email) {
this.name = name;
this.email = email;
}
setTaxData(ssn, salary) {
this.taxData = new EmployeeTaxData(ssn, salary);
}
// ...
}
关于下面的代码,我想用最简单的eloquent方式让Hero
[=27=创建的对象可以使用spin()方法] 所以它使用的是共享原型。我想与其他也需要它的对象分享这个方法。
不幸的是,我无法使我的代码正常工作,因为 this.angle
不是指它需要的 Hero
class,而是 Spinner
class?
class Spinner {
constructor(direction){
this.direction = direction;
}
spin(direction) {
switch (direction) {
case 'left':
this.angle -= 1;
break;
case 'right':
this.angle += 1;
break;
// no default
}
}
}
class Hero {
constructor(xPosition, yPosition) {
this.description = 'hero';
this.width = 25;
this.height = 50;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.angle = 0;
this.color = 'red';
this.spin = new Spinner();
}
spin() {
this.spin.spin();
}
}
const heroClass = new Hero(100, 200);
console.log(heroClass.angle); // result is 0
heroClass.spin.spin('left');
console.log(heroClass.angle); // result is STILL 0, it didn't work
...the this.angle is not referring to to the Hero class that it needs to, but the Spinner class?
应该的。当你在微调器 class 内部时,那么 this
指的是一个微调器对象,这也意味着 this.angle
在微调器内部 class 指的是一个角度 属性微调对象。
您可能希望旋转器 return 一个新的角度值,那么使用旋转器的英雄对象应该保存 return 编辑的新角度值。
class Spinner {
constructor(direction){
this.direction = direction;
}
spin(direction, angle) {
switch (direction) {
case 'left':
angle -= 1;
break;
case 'right':
angle += 1;
break;
// no default
}
return angle;
}
}
class Hero {
constructor(xPosition, yPosition) {
this.description = 'hero';
this.width = 25;
this.height = 50;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.angle = 0;
this.color = 'red';
this.spinner = new Spinner();
}
spin(direction) {
this.angle = this.spinner.spin(direction, this.angle);
}
}
const heroClass = new Hero(100, 200);
console.log(heroClass.angle); // result is 0
heroClass.spin('left');
console.log(heroClass.angle); // result is -1
我必须做一些其他的小改动才能让它起作用。例如,您有一个名为 "spin" this.spin = new Spinner
的数据 属性 以及一个名为 spin spin() {
的方法。他们互相压倒。
我正在努力在 ES6 中实现组合 classes!我试图同时理解很多事情,可能犯了一个愚蠢的错误。
我想暂时避免使用 extend
和 super
的继承,并且找到了这个很好的组合示例,它似乎展示了我所追求的:
class EmployeeTaxData {
constructor(ssn, salary) {
this.ssn = ssn;
this.salary = salary;
}
// ...
}
class Employee {
constructor(name, email) {
this.name = name;
this.email = email;
}
setTaxData(ssn, salary) {
this.taxData = new EmployeeTaxData(ssn, salary);
}
// ...
}
关于下面的代码,我想用最简单的eloquent方式让Hero
[=27=创建的对象可以使用spin()方法] 所以它使用的是共享原型。我想与其他也需要它的对象分享这个方法。
不幸的是,我无法使我的代码正常工作,因为 this.angle
不是指它需要的 Hero
class,而是 Spinner
class?
class Spinner {
constructor(direction){
this.direction = direction;
}
spin(direction) {
switch (direction) {
case 'left':
this.angle -= 1;
break;
case 'right':
this.angle += 1;
break;
// no default
}
}
}
class Hero {
constructor(xPosition, yPosition) {
this.description = 'hero';
this.width = 25;
this.height = 50;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.angle = 0;
this.color = 'red';
this.spin = new Spinner();
}
spin() {
this.spin.spin();
}
}
const heroClass = new Hero(100, 200);
console.log(heroClass.angle); // result is 0
heroClass.spin.spin('left');
console.log(heroClass.angle); // result is STILL 0, it didn't work
...the this.angle is not referring to to the Hero class that it needs to, but the Spinner class?
应该的。当你在微调器 class 内部时,那么 this
指的是一个微调器对象,这也意味着 this.angle
在微调器内部 class 指的是一个角度 属性微调对象。
您可能希望旋转器 return 一个新的角度值,那么使用旋转器的英雄对象应该保存 return 编辑的新角度值。
class Spinner {
constructor(direction){
this.direction = direction;
}
spin(direction, angle) {
switch (direction) {
case 'left':
angle -= 1;
break;
case 'right':
angle += 1;
break;
// no default
}
return angle;
}
}
class Hero {
constructor(xPosition, yPosition) {
this.description = 'hero';
this.width = 25;
this.height = 50;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.angle = 0;
this.color = 'red';
this.spinner = new Spinner();
}
spin(direction) {
this.angle = this.spinner.spin(direction, this.angle);
}
}
const heroClass = new Hero(100, 200);
console.log(heroClass.angle); // result is 0
heroClass.spin('left');
console.log(heroClass.angle); // result is -1
我必须做一些其他的小改动才能让它起作用。例如,您有一个名为 "spin" this.spin = new Spinner
的数据 属性 以及一个名为 spin spin() {
的方法。他们互相压倒。