2016-07-02 4 views
0

私は、静的な関数の構文を使用して、ここで完璧な作品を探しています、しかし、私はクラス定義自体の内部(すなわちclassName.staticFunction() => ...代わりstatic staticFunction =() => ...のコンストラクタ自体で宣言静的関数を好きな機能を。静的関数を使用せずにこの高次関数を書き直すにはどうすればよいですか?

ここでコードがあります私は、下図のように、私は、コンストラクタ/機能ではなく、static構文で定義された静的な関数を使用するためにリファクタリングしたいことに言及しています。

const higherOrderFunction = another => andAnother => class extends Component { 

    static functionName = { 
    test: React.PropTypes.object 
    }; 

    constructor(props) { 
    super(props); 
    } 

    render() { 
    return <h1>Hello, World!</h1>; 
    } 
}; 

export default higherOrderFunction; 
+0

? – Bergi

答えて

1

classの値は、コンストラクタと同じですfunctionあなたはクラスなしで定義します。したがって:

const higherOrderFunction = another => andAnother => Component; 

function Component() {} 

Component.functionName =() => { 
    test: React.PropTypes.object 
}; 

Component.prototype.render =() => { 
    return <h1>Hello, World!</h1>; 
} 

export default higherOrderFunction; 

あなたは、任意の引数をカプセル化し、使用する関数本体内の関数とメンバーの定義をラップすることもできます。あなたが `another`と` andAnother`を使用している

const higherOrderFunction = another => andAnother => { 
    function Component() {} 

    Component.functionName =() => { 
    test: React.PropTypes.object 
    }; 

    Component.prototype.render =() => { 
    return <h1>Hello, World! {another} and {andAnother}</h1>; 
    } 

    return Component; 
}; 

export default higherOrderFunction; 
+0

これは理にかなっていますが、私はES6クラスでこれを達成することができればと思っていました。 – Detuned

+0

はい。関数を定義する代わりに、クラスを定義するだけです。静的関数をあとでタックすることもできます。 – DDS

関連する問題