configuration - 在高级模式下编译时对象被删除或部分折叠 - Google Closure Compiler

标签 configuration namespaces annotations google-closure-compiler prototypal-inheritance

我在 Closure 中使用 angular,但在使用 @export 注释时遇到了问题。特别是能够导出属性定义。我以为我可以使用以下代码进行操作,但它似乎非常喜怒无常。

/**
 * @export
 * @constructor
 */
com.MyController = function() {
   this.greeting = 'hello';
   this.goodbye = 'bye'
};

/**
 * @export
 */
com.MyController.prototype = {

    'sayGoodbye': function() {
        return this.goodbye;
    },

    'sayHello': function() {
        return this.greeting;
    }
};

有时编译器会导出原型(prototype)上的所有方法,有时它只会导出'sayGoodbye',有时它会完全折叠原型(prototype)上定义的整个对象。

我知道我可以这样做:
/**
 * @export
 */
com.MyController.prototype.sayHello = function() {
    return this.greeting;
};

哪个有效,但是一直输入它开始变得非常费力,而且可读性较差。

我知道编译器有一个选项来 setExportLocalPropertyDefinitions,但是代码的开源版本目前没有设置这个标志 - 我一直在弄乱 Java 源代码来尝试设置它,但没有运气所以远的。

更新:
因此,如果在不相关的类中存在另一个同名的方法,则原型(prototype)上的方法(例如 sayGoodbye)将被导出。我想它被导出不是因为它像我最初希望的那样定义为一个字符串,而是因为这两种不相关的方法之间存在一些混淆?

最佳答案

确保 export 的方法签名定义为:

Code that uses the @export annotation must either

  • include closure/base.js, or,

  • define both goog.exportSymbol and goog.exportProperty with the same method signature in their own codebase.


引用文献
  • Annotating JavaScript for the Closure Compiler: @export
  • 关于configuration - 在高级模式下编译时对象被删除或部分折叠 - Google Closure Compiler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27147909/

    相关文章:

    php - 如何识别 PHP 是作为模块安装还是作为 CGI 安装

    xml - E4X:抓取带有命名空间的节点?

    java - 自定义注释以抑制特定的 FindBugs 警告

    python - 执行前模块命名空间初始化

    java - @RequestMapping 在渲染方法中带有 2 个参数

    java - Singleton bean 中的请求作用域字段

    configuration - 每小时 log4net RollingFileAppender

    visual-studio-code - 如何在 VS Code 中创建 "terminal configuration macro"?轻松打开和拆分多个终端

    java - log4j2 文件追加器用于文件编码/格式的是什么?

    c++ - 没有歧义的命名空间规范