抽象类在JavaScript中

huangapple go评论87阅读模式
英文:

Abstract class in JavaScript

问题

我之前用JAVA做过这个,有一些类:

- defaultprinter.java
- hpprinter.java
- epsonprinter.java
...

defaultprinter.java类有一个根据打印机不同可能不同的方法

public void serial() {
  根据品牌获取序列号的方法()
}

然后我有一个方法来选择要选择哪个打印机

if (brand.equals("hp")) {
  printer = new hpprinter()
} ...

我该如何在JavaScript中实现这一点?
我希望每个打印机都分割在单独的文件中

- defaultprinter.js
- hpprinter.js
- epsonprinter.js
...

我如何拥有通用且可重写的方法

const serial = () => {
  根据品牌获取序列号的方法()
}

以及如何选择我想要的打印机?

if (brand == "hp") {
  printer = ??????
} ...

谢谢你的帮助 抽象类在JavaScript中

英文:

I have done this in JAVA some time ago, I had some classes:

-defaultprinter.java
-hpprinter.java
-epsonprinter.java
...

the defaultprinter.java class has a method that can be different on each printer

public void serial() {
  get-serial-depending-on-brand-method()
}

then I have a method that will select what printer to select

if(brand.equals("hp"){
  printer = new hpprinter()
} ...

How can I do this with Javascript?
I want to have every printer splited in separated files

-defaultprinter.js
-hpprinter.js
-epsonprinter.js
...

How can I have the common and overridable method

const serial = () => {
  get-serial-depending-on-brand-method()
}

And how can I select the printer that I want?

if(brand == "hp") {
  printer = ??????
} ...

Thanks for the help 抽象类在JavaScript中

答案1

得分: 1

首先,您需要模块,可以是CommonJS风格ES6模块风格

之后,您可以定义普通的javascript类并进行子类化操作。

--

如果您是第一次使用Electron,但具有一些现代Web开发背景,请查看electron-webpack项目。它需要一些默认的结构,但值得努力与之保持一致。

这里有一个小例子

英文:

First you need modules, either commons js style or es6 modules style.

After that, you can define regular javascript classes and do the subclassing thing.

--

If you doing electron for the first time but have some modern web development background, take a look at the electron-webpack project. It demands some default structure, but it worths the effort to get aligned with it.

here goes a little example!

huangapple
  • 本文由 发表于 2020年3月4日 09:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/60517734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定