This is a helper function I define for creating properties in my classes:
###
# defines a property on an object/class
# modified from https://gist.github.com/746380
###
Object::public = (name, options = {}) ->
options['default'] ?= null
variable = options['default']
getter = if typeof options['get'] is 'function' then options['get'] else -> variable
setter = if typeof options['set'] is 'function' then options['set'] else (value) -> variable = value
config = {}
config['get'] = getter if options['get']
config['set'] = setter if options['set']
config['configurable'] = no
config['enumerable'] = yes
Object.defineProperty @prototype, name, config
Inside a file I have the two classes below, Folds and _Folds, the latter being hidden with only the former exported (namespaced) to the global.
###
Public exported fetcher for fold factory,
being the only way through which to create folds.
###
class Folds
_instance = undefined
# static fetch property method
@public 'Factory',
get: -> _instance ?= new _Folds
###
Encapsuled singleton factory for one-stop fold creation
###
class _Folds
constructor: ->
create: -> new Fold
Then when I try this, it returns false. Why?
console.log 'Factory' of Folds
The following is returning “function Folds() {}”
console.log Folds
I can’t call Folds.Factory.create() because Folds.Factory is undefined.
CoffeeScript’s
inis for arrays (and array-like objects);ofcompiles to JavaScript’sin. So what you want isThat’s not the core problem, though: The core problem is that the
publicmethod you’re using actually defines a property with the given name on the class’ prototype, as the linetells us. So what you really want is
which means that the
Factorymethod will be available as a property of everyFoldsinstance.