[재미로 하는 투표] module.exports 에 속하는 함수에서 다른 함수 접근하는 방법

아래 코드는 exports.default로 내보내기하는 a,b 함수가 있을 때 a 함수에서 b 함수를 호출하는 다양한 방식의 코드입니다. 어떤 스타일을 선호하세요?
(아래 예제 출처는 예제 출처 : https://gist.github.com/kimmobrunfeldt/10848413 입니다. 여기에는 es6 모듈 방법은 없습니다. :slight_smile: )

  • Style 1 : Export all manually
  • Style 2 : Write all in module.exports
  • Style 3 : Export ‘automatically’ while writing functions
  • Style 4 : Good for a utils module

0 투표자

Style 1 : Export all manually

// Good: Calling functions inside the module is convenient
// Bad:  module.exports becomes verbose and it's tedious to add new functions

function a() {
    b()
}

function b() {
}

module.exports = {
    a: a,
    b: b
}

Style 2 : Write all in module.exports

// Good: All functions in the same "package" (only good I could figure out)
// Bad:  Hard to read. Wider indentation. Calling other functions is tedious

module.exports = {
    a: function() {
        module.exports.b()
    },
    b: function() {
    }
}

Style 3 : Export ‘automatically’ while writing functions

// Good: module.exports is clean and no hassle needed when adding functions
// Bad: Calling function inside the module is verbose and inconvenient

var exports = {}

exports.a = function() {
    exports.b()
}

exports.b = function() {
}

module.exports = exports

Style 4 : Good for a utils module

// Good: Calling functions inside module is convenient and module.exports is clean
// Bad:  Syntax is not so clear, though that is arguable

var exports = {}

var a = exports.a = function() {
    b()
}

var b = exports.b = function() {
}

module.exports = exports