Javascript ES6快速和静态方法使用问题
我正在尝试学习express,并希望与babel一起使用ES6。我的问题是当我使用静态方法处理像bellow这样的请求时Javascript ES6快速和静态方法使用问题,javascript,node.js,express,babeljs,Javascript,Node.js,Express,Babeljs,我正在尝试学习express,并希望与babel一起使用ES6。我的问题是当我使用静态方法处理像bellow这样的请求时 class MyCtrl { static index (req,res) { res.status(200).send({data:"json data"}); } } router.get('/', MyCtrl.index) 我想知道,这(使用静态方法)会影响性能吗?我对Node.js运行时不太了解,但正如我所知,在某些语言(如C#)
class MyCtrl {
static index (req,res) {
res.status(200).send({data:"json data"});
}
}
router.get('/', MyCtrl.index)
我想知道,这(使用静态方法)会影响性能吗?我对Node.js运行时不太了解,但正如我所知,在某些语言(如C#)中经常使用静态方法并不是一件好事
或者ES6类还有其他合适的方法来实现这一点。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的原型。就你的问题而言,你所做的只是将一个常规函数传递给路由器。获取,这样就不会有任何性能差异。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的原型。因此,就您的问题而言,您所做的只是将一个常规函数传递给
路由器。获取,这样就不会有任何性能差异。谢谢。你的回答有道理。谢谢。你的回答有道理。