0
我有代碼看起來像這樣的模塊中:JavaScript性能
var MyModule = module.exports;
MyModule.some_function = function(arg) {
// Do some code here
// Do some logging using the function name
// var function_name = calleeArgs.callee.toString().match(/function ([^\(]+)/)[1];
// BAD, this method doesnt have a name
};
上面的代碼作爲函數沒有一個名字不起作用。 作爲替代方案,我能做到以下幾點,在這種情況下,日誌將包含方法名稱:
function _some_function(arg) {
// Do some code here
// Do some logging using the function name - BAD, this method doesnt have a name
// var function_name = calleeArgs.callee.toString().match(/function ([^\(]+)/)[1];
// GOO, this method doesnt have a name
}
MyModule.some_function = function(arg) {
_some_function(arg);
};
所以我的問題是:
1)是否寫這樣任何意義 - 據我瞭解_some_function()是本地模塊,所以就全球範圍/訪問而言,不會有負面影響
2.)這是否(第二個選項)有任何性能影響? (我的猜測當然不會,或者至少可以忽略不計)?
您可以在JS中使用命名函數表達式。而使用Node,你甚至不用擔心IE的bug。 – Scimonster
無需包裝它 - 在第一種情況下,您可以使用:'MyModule.some_function = _some_function;'或第二種情況下的'MyModule.some_function = function some_function(...){}'。 – dc5
謝謝!顯而易見的解決方案 - 不用於JavaScript語法... –