私はこのような一つの大きなファイルにクラスがあるとします。分割までのクラス定義を
export default class {
constructor() {}
methodA() {}
methodB() {}
methodC() {}
}
そして私はmethodA
、methodB
、およびmethodC
がそれぞれになるようにクラス定義を分割したいです独自のファイルで定義されています。これは可能ですか?
私はこのような一つの大きなファイルにクラスがあるとします。分割までのクラス定義を
export default class {
constructor() {}
methodA() {}
methodB() {}
methodC() {}
}
そして私はmethodA
、methodB
、およびmethodC
がそれぞれになるようにクラス定義を分割したいです独自のファイルで定義されています。これは可能ですか?
class
はいつものプロトタイプのワークフローのための構文砂糖ことになっているとしてあなたは、ことができるようになります。
import methodOne from 'methodOne'
import methodTwo from 'methodTwo'
class MyClass {
constructor() {
}
}
Object.assign(MyClass.prototype, {methodOne, methodTwo})
export default MyClass
@elclanrsは、正しい答えを与えたが、私はの使用を許可するように変更しますthis
。私はこれがもっと読みやすいと思う。
import methodOne from 'methodOne'
import methodTwo from 'methodTwo'
class MyClass {
constructor() {
this.methodOne = methodOne.bind(this)
this.methodTwo = methodTwo.bind(this)
}
}
export default MyClass
ヒント:あなたのクラスは非常に大きい場合には、複数のファイルに分割されたワラントことが、よりよい解決策は、複数のクラスにクラスを分割するかもしれません。
しかし、プロパティを持つメソッドが必要な場合はどうすればよいですか? methodOne(a、b) – marschro
'methodOne'' methodTwo'の使い方は? – GeminiYellow