Reputation: 3
I'm trying to learn express and wanted to use ES6 with babel. My questions is When I use static methods for handling requests like bellow ;
class MyCtrl {
static index (req,res) {
res.status(200).send({data:"json data"});
}
}
router.get('/', MyCtrl.index)
I was wondering, will this(using static methods ) be effecting performance? I do not have much knowledge of Node.js runtime but as I know using static methods so often in some language (like C#) is not a good thing.
Or is there another proper way to doing this with ES6 classes .
Upvotes: 0
Views: 78
Reputation: 53839
ES6 classes aren't really some new structure, it's just some syntax sugar for JavaScript's prototypal model.
So when you do something like this:
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}`);
}
}
Behind the scenes, it basically just translates to this:
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}`);
};
So it's a convenient shorthand, but it's still just using JavaScript's prototypal stuff. So as far as your question goes, all you're doing is passing a regular function to router.get
so there isn't any performance difference.
Upvotes: 2