2016-04-26 10 views
0

私は急行を学びたいと思っており、ES6をbabelと使いたいと思っていました。私の質問は以下のような要求を処理する静的メソッドを使用するときです。ES6 Expressと静的メソッドの使用に関する質問

class MyCtrl { 

    static index (req,res) { 
     res.status(200).send({data:"json data"}); 
    } 
} 

router.get('/', MyCtrl.index) 

これは(静的メソッドを使用して)パフォーマンスに影響しますか?私はNode.jsのランタイムについて多くの知識は持っていませんが、静的メソッドを使用することを知っているので、C#のような言語ではしばしば良いことではありません。

また、ES6クラスでこれを行うもう1つの適切な方法があります。

答えて

2

ES6クラスは実際には新しい構造ではなく、JavaScriptのプロトタイプモデル用の構文砂糖です。

あなたはこのような何かときに:舞台裏

class Animal { 
    constructor(name, age) { 
    this.name = name; 
    this.age = age; 
    } 

    static printAllowedTypes() { 
    console.log('Rabbit, Dog, Cat, Monkey'); 
    } 

    printName() { 
    console.log(`Name: ${this.name}`); 
    } 

    printAge() { 
    console.log(`Age: ${this.age}`); 
    } 
} 

を、それは基本的にはこれに変換します。

function Animal(name, age) { 
    this.name = name; 
    this.age = age; 
} 

Animal.printAllowedTypes = function() { 
    console.log('Rabbit, Dog, Cat, Monkey'); 
}; 

Animal.prototype.printName = function() { 
    console.log(`Name: ${this.name}`); 
}; 

Animal.prototype.printAge = function() { 
    console.log(`Age: ${this.age}`); 
}; 

だから、便利な短縮形だが、それはまだただのJavaScriptの原型を使用していますもの。だからあなたの質問が行く限り、あなたがやっているのは普通の関数をrouter.getに渡すことなので、パフォーマンスの違いはありません。

+0

ありがとうございました。あなたの答えは理にかなっています。 –

関連する問題