在 CoffeeScript 中为我的单例类定义属性 getter

发布于 2024-12-20 10:53:09 字数 1332 浏览 0 评论 0原文

这是我为在类中创建属性而定义的辅助函数:

###
# 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-12-27 10:53:09

CoffeeScript 的 in 用于数组(和类似数组的对象); of 编译为 JavaScript 的 in。所以你想要的是

console.log 'Factory' of Folds

,但这不是核心问题:核心问题是你使用的 public 方法实际上在类的原型上定义了一个具有给定名称的属性。 em>,正如该行

Object.defineProperty @prototype, name, config

告诉我们的那样。因此,您真正想要的是

console.log 'Factory' of Folds.prototype  # should be true

Factory 方法将作为每个 Folds 实例的属性提供。

CoffeeScript's in is for arrays (and array-like objects); of compiles to JavaScript's in. So what you want is

console.log 'Factory' of Folds

That'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 line

Object.defineProperty @prototype, name, config

tells us. So what you really want is

console.log 'Factory' of Folds.prototype  # should be true

which means that the Factory method will be available as a property of every Folds instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文