2015-01-15 14 views
7

私はこのような一つの大きなファイルにクラスがあるとします。分割までのクラス定義を

export default class { 
    constructor() {} 
    methodA() {} 
    methodB() {} 
    methodC() {} 
} 

そして私はmethodAmethodB、およびmethodCがそれぞれになるようにクラス定義を分割したいです独自のファイルで定義されています。これは可能ですか?

答えて

6

classはいつものプロトタイプのワークフローのための構文砂糖ことになっているとしてあなたは、ことができるようになります。

import methodOne from 'methodOne' 
import methodTwo from 'methodTwo' 

class MyClass { 
    constructor() { 
    } 
} 

Object.assign(MyClass.prototype, {methodOne, methodTwo}) 

export default MyClass 
+0

しかし、プロパティを持つメソッドが必要な場合はどうすればよいですか? methodOne(a、b) – marschro

+0

'methodOne'' methodTwo'の使い方は? – GeminiYellow

1

@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 

ヒント:あなたのクラスは非常に大きい場合には、複数のファイルに分割されたワラントことが、よりよい解決策は、複数のクラスにクラスを分割するかもしれません。

関連する問題