Is it a good idea to use self-invoking functions to hide functions and variables like this?
Is there a better way?
Is it slower than declaring functions and variables on the same level?
validator = require 'json-schema'
actionA = do ->
schema =
type : 'object'
properties:
param1 : { type : 'string', required : true }
param2 : { type : 'string', required : true }
encode = (results) -> JSON.parse(results)
(params, callback) ->
res = validator.validate params, schema
value = if res.valid then encode(params) else false
callback value
actionB = do ->
schema =
type : 'object'
properties:
param1 : { type : 'integer', required : true }
param2 : { type : 'integer', optional : true }
encode = (results) -> JSON.stringify(results)
(params, callback) ->
res = validator.validate params, schema
value = if res.valid then encode(params) else false
callback value
Thank you for your help.
Yes, I believe it’s fine. In fact, when you create “classes” in CoffeeScript
it compiles to this:
Which primarily does the same thing as the code you posted.