使用 Google Closure 定义配置对象的最佳方法

发布于 2024-09-29 19:27:35 字数 761 浏览 0 评论 0原文

我喜欢 Google Closure 编译器如何优化代码中的符号。但是,我还没有找到一种好方法来定义将配置对象作为参数的公共导出函数。考虑以下代码片段:

goog.provide('foo');
goog.require('goog.dom');

/** @typedef {{
 *              id : string,
 *              clazz : string
 *           }}
 */
foo.config;

/**
 * Does some neat stuff
 * @param {foo.config} config
 */    
foo.myFoo = function(config) {
    var el = goog.dom.getElement(config.id);
    goog.dom.classes.add(el, config.clazz);
} 
goog.exportSymbol('foo.myFoo', foo.myFoo);

现在假设我们加载此脚本,并希望按如下方式调用 myFoo:

<script type="text/javascript">
foo.myFoo({
    id: 'unique-id',
    clazz: 'pretty'
});
</script>

如果编译,这将失败,因为 id 和 clazz 属性被压缩。

有谁知道使用 Google Closure 编译器实现和导出配置对象的优雅方法?

I love how Google Closure compiler will optimize symbols in code. However, I have not found a good way to define public, exported functions that take configuration objects as parameters. Consider this code snippet:

goog.provide('foo');
goog.require('goog.dom');

/** @typedef {{
 *              id : string,
 *              clazz : string
 *           }}
 */
foo.config;

/**
 * Does some neat stuff
 * @param {foo.config} config
 */    
foo.myFoo = function(config) {
    var el = goog.dom.getElement(config.id);
    goog.dom.classes.add(el, config.clazz);
} 
goog.exportSymbol('foo.myFoo', foo.myFoo);

Now assume we load this script, and want to call myFoo as follows:

<script type="text/javascript">
foo.myFoo({
    id: 'unique-id',
    clazz: 'pretty'
});
</script>

If compiled, this would fail because id and clazz properties were compressed.

Does anyone know of an elegant way to implement and export configuration objects using the Google Closure compiler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

口干舌燥 2024-10-06 19:27:35

我的建议是简单地将参数注释为 {Object} 并引用键,如下所示:

foo.myFoo({
    'id': 'unique-id',
    'clazz': 'pretty'
});

...

/**
 * Does some neat stuff
 * @param {Object} config
 */    
foo.myFoo = function(config) {
    var el = goog.dom.getElement(config['id']);
    goog.dom.classes.add(el, config['clazz']);
} 

My suggestion would be to simply annotate the parameter as {Object} and quote the keys, as below:

foo.myFoo({
    'id': 'unique-id',
    'clazz': 'pretty'
});

...

/**
 * Does some neat stuff
 * @param {Object} config
 */    
foo.myFoo = function(config) {
    var el = goog.dom.getElement(config['id']);
    goog.dom.classes.add(el, config['clazz']);
} 
寻找一个思念的角度 2024-10-06 19:27:35

关于损坏的属性名称和原始属性名称的冲突

您必须执行以下任一操作:

  • “外部”属性名称(以便它们不会被重命名)
  • 使用“括号符号”来访问属性

关于配置对象

根据 Closure Compiler 文档,目前你还不能这样做。您必须将参数标记为对象。

可以定义一个类型,但这对您没有帮助,因为类型需要所有属性都存在,但您只能在配置对象中设置几个参数。

Closure Compiler 文档建议您将参数标记为对象,但在注释中记录字段。

Regarding conflicts of mangled and raw property names

You'll have to either:

  • "extern" your property names (so that they won't be renamed)
  • use "bracketed notation" to access propertiies

Regarding configuration objects

According to the Closure Compiler docs, currently you can't do it. You have to mark the param as Object.

You may define a type, but it won't help you because a type requires all the properties to exist, but you may only set a few parameters in a configuration object.

The Closure Compiler documentation recommends that you mark the parameter as Object, but document the fields in comments.

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