在 CoffeeScript 中为我的单例类定义属性 getter
这是我为在类中创建属性而定义的辅助函数:
###
# 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
在文件内,我有以下两个类:Folds 和 _Folds,后者被隐藏,仅前者导出(命名空间)到全局。
###
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
然后当我尝试这个时,它返回 false。为什么?
console.log 'Factory' of Folds
以下返回“function Folds() {}”
console.log Folds
我无法调用 Folds.Factory.create() 因为 Folds.Factory 未定义。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CoffeeScript 的
in
用于数组(和类似数组的对象);of
编译为 JavaScript 的in
。所以你想要的是,但这不是核心问题:核心问题是你使用的
public
方法实际上在类的原型上定义了一个具有给定名称的属性。 em>,正如该行告诉我们的那样。因此,您真正想要的是
,
Factory
方法将作为每个Folds
实例的属性提供。CoffeeScript's
in
is for arrays (and array-like objects);of
compiles to JavaScript'sin
. So what you want isThat's not the core problem, though: The core problem is that the
public
method 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
Factory
method will be available as a property of everyFolds
instance.