javascript - 如何同时使用 goog.provide 和 @externs?

标签 javascript google-closure-compiler google-closure-library

我试图通过解决缩小问题来稍微简化我们的代码,这些问题往往会让我们使用类似 oVar["stuff"]["things"] 而不是 oVar。东西.things

我在使用 @externs 注释的同时还使用了 goog.provide 时遇到了问题。如果我手动将新文件作为 --externs 传递,或者我使用 @externs 并将新文件作为 --js 传递,我已经让我的类(class)按照我想要的方式工作。这两个解决方案的问题是我必须为我创建的每个“外部”类添加 --externs 或 --js。

一旦我尝试在类文件中添加 goog.provide 或 goog.module(即使不使用 goog.require 在文件中加载),我使用的所有 externs 选项都停止工作。澄清一下,在这种情况下使用 goog.require 根本没有帮助。

我添加了一些伪代码来模仿我正在做的事情。基本上,我有一个函数,实现者可以用一个不需要缩小任何参数的对象来调用。

这是我在类文件中尝试做的事情:

/**
 * @fileoverview
 * @externs
 */

goog.provide("common.classes.newClass");
/**
 *
 * @constructor
 */
common.classes.newClass = function() {};

/**
 * @typedef {*}
 */
common.classes.newClass.prototype.var1;
/**
 * @typedef {*}
 */
common.classes.newClass.prototype.var2;

下面是我尝试在另一个文件中使用它的方式:

goog.provide("app.namespace");

goog.require("common.classes.newClass");

/**
 * @param {common.classes.newClass} parameter
 * @exports
 */
app.namespace.init = function(parameter)
{
    // do stuff

    /**
     * @type {common.classes.newClass}
     */
    this.passedObject = parameter;

    // do other stuff

    otherFunction(this.passedObject.var1);
};

我遇到的问题是,除非我使用 this.passedObject['var1'],否则 'var1' 键会被缩小。

最佳答案

你不能。这些用于 2 个完全独立的事物。您也不要导入 extern 或使用 goog.require 引用它。外部人员是全局性的。

要在外部声明命名空间,只需使用:

const common = {};
/** @const */ common.classes = {};

关于javascript - 如何同时使用 goog.provide 和 @externs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49440229/

相关文章:

javascript - Angular 4 : Can't bind to 'ngModel'

javascript - ChartJs Donut 解析数据集

javascript - 如何设置:hover as default and reset once moused over?

javascript - 与 Closure Compiler 一起工作的 JavaScript 类的实现

javascript - Make Closure Compiler strip 日志函数用法

javascript - 如何将 goog.ui.Autocomplete 最小输入设置为 0

javascript - JavaScript 是否支持按字符串格式格式化数字?

java - 如何在java中使用 "closure-compiler "

html - 如何在完全离线的工作空间中设置 LimeJS

touch - 移动触摸支持——在 Closure-library 中