首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类/模块类中的ES6类

类/模块类中的ES6类
EN

Stack Overflow用户
提问于 2017-08-22 21:00:51
回答 3查看 86关注 0票数 0

我想要创建一个模块化的类。它应该可以像下面的例子一样使用,并且应该可以分离成文件。子类(Tasks)应该可以访问父类(Foo)的方法。

代码语言:javascript
复制
// Use basic methods
const foo = new Foo();
foo.connect();
foo.authenticate('user', 'password');

// Use task (or any other) module 
const tasks = foo.Tasks;
tasks.createTask('ask on stackoverflow');
tasks.updateTask({ done: true });

我只做了下面的工作。但是,由于我必须使用Bar关键字启动一个新的new实例,所以无法访问更改后的this.output值。如何省略new关键字,并使用上面所需的语法与foo使用相同的实例?

代码语言:javascript
复制
const Foo = class Foo {
  constructor() {
    this.output = 1;
  }

  changeOutput(output) {
    this.output = output;
  }
}

Foo.Bar = class Bar extends Foo {
  getOutput() {
    return this.output;
  }
}

const foo = new Foo();
foo.changeOutput(2);

const bar = new Foo.Bar(); // Creates a new instance what is wrong, but foo.Bar() or anything else doesn't work (Error: is not a function).
console.log(bar.getOutput()); // Result is 1, but should be 2

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-22 21:29:04

我还是不知道你在找什么,但似乎

继承

代码语言:javascript
复制
class Foo {
  constructor() {
    this.output = 1;
  }
  changeOutput(output) {
    this.output = output;
  }
}

class Bar extends Foo {
  getOutput() {
    return this.output;
  }
}

const x = new Bar();
x.changeOutput(2);
console.log(x.getOutput());

作文

代码语言:javascript
复制
class Foo {
  constructor() {
    this.output = 1;
    this.bar = new (new.target).Bar(this);
  }
  changeOutput(output) {
    this.output = output;
  }
}

Foo.Bar = class Bar {
  constructor(foo) {
    this.foo = foo;
  }
  getOutput() {
    return this.foo.output;
  }
}

const foo = new Foo();
foo.changeOutput(2);

const bar = foo.bar;
console.log(bar.getOutput());
票数 1
EN

Stack Overflow用户

发布于 2017-08-22 21:08:42

我从未见过任何面向对象的语言以您正在尝试的方式工作,javascript也是如此。

Foo.Bar是Foo上的一个静态方法,即扩展类Foo,而不是实例foo。这就是为什么你有数字1出现。

分离您的关注点,并将更改应用于正在使用的实例,即具有任务的实例。

代码语言:javascript
复制
class Bar extends Foo {
  getOutput() {
    return this.output;
  }
}
var bar = new Bar();
bar.changeOutput(2)
bar.getOutput() //2

但是..。

您可以编辑this的绑定以实现您想要的结果,但首先遵循糟糕的OOP设计模式并不是一个好的实践(您可以通过其他范例或将这两个类合并在一起来实现您想要的结果)。

代码语言:javascript
复制
const Foo = class Foo {
  constructor() {
    this.output = 1;
  }

  changeOutput(output) {
    this.output = output;
  }
}

Foo.Bar = class Bar extends Foo {
  getOutput() {
    return this.output;
  }
}

const foo = new Foo();
foo.changeOutput(2);

const bar = new Foo.Bar();

console.log(bar.getOutput.call(foo)); // Result is 2 now

票数 1
EN

Stack Overflow用户

发布于 2017-08-22 21:06:40

代码语言:javascript
复制
class Foo {
  constructor() {
    this.output = 1;
  }

  changeOutput(output) {
    this.output = output;
  }
}

class Bar extends Foo {
  getOutput() {
    return this.output;
  }
}

const foo = new Foo();
foo.changeOutput(2);

const bar = new Bar(); 
bar.changeOutput(2);

console.log(bar.getOutput()); //

当您创建子实例时,Bar将调用Foo的构造函数并将输出值初始化为1,当您调用getOutput方法时,它将更改bar的输出值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45826909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档