// constructor function implementation
function Test (args) {
// explicit internal state without using `this`
const state = {
op: args.op,
missing: args.missing
}
return { // explicit return
// explicit binding of function
// parameters with internal state
// or only whatever it needs
go: go.bind(null, state)
}
}
// functions defined separately
// can't (and shouldn't) alter object state
// should only act on inputs and return
function go (options) {
return `${options.op} without: ${options.missing}`
}
// during normal use
const options = {op: 'running', missing: 'this'}
const test = Test(options) // `new` is optional
console.log('Normal use and', test.go())
// testing the function directly
const testOptions = {op: 'Testing', missing: 'object'}
const expected = 'Testing without: object'
const actual = go(testOptions)
console.log(actual)
console.log('Test passes:', actual === expected)