将module.exports
放在文件顶部是否可以接受?我正在努力坚持“首屏”概念,以便在打开NodeJS文件时所有功能都出现在视线范围内。
示例:
'use strict';
module.exports = {
doSomething : doSomething,
doSomethingElse : doSomethingElse
};
var _ = require('lodash'),
moment = require('moment');
function doSomething (value) {
console.log('doSomething called with ' + value);
}
function doSomethingElse () {
console.log('doSomethingElse called');
}
...或者......或者......
'use strict';
var service = {
CONSTANT_VAR : 'blah',
doSomething : doSomething,
doSomethingElse : doSomethingElse
};
var _ = require('lodash'),
moment = require('moment');
function doSomething (value) {
console.log('doSomething called with ' + value);
}
function doSomethingElse () {
console.log('doSomethingElse called');
}
module.exports = service;
答案 0 :(得分:1)
绝对。 Functions are hoisted to the top因此,您可以在需要时随时使用它们。如果你喜欢那种风格,那就去吧。
吊装示例:
writeMessage('Hey!');
function writeMessage(msg) {
document.querySelector('pre').innerText = msg;
}

<pre></pre>
&#13;