我遇到这样的情况:
class TestClass {
constructor() {
shared logic
}
anotherFunction() {
shared logic
}
}
我如何在不复制代码的情况下实现这一点?
一如既往,为共享逻辑创建一个函数,无论是在类内部还是在类外部。
class TestClass {
constructor() {
this.sharedLogicFunction();
}
anotherFunction() {
this.sharedLogicFunction();
}
sharedLogicFunction() {}
}
将代码放入anotherFunction()
并从构造函数调用此函数。
class TestClass {
constructor() {
this.anotherFunction();
}
anotherFunction() {
here is some logic to do
}
}