【问题标题】:Closure Compiler JsDoc annotations to replace tests in source code?Closure Compiler JsDoc 注解替换源代码中的测试?
【发布时间】:2017-04-05 00:11:29
【问题描述】:

JSDoc 注解是否足以测试参数的有效性?

示例:在下面的“add”方法中,参数(Point 对象)被定义为Non-nullable Point 类型。在此方法中并使用 Closure Compiler,我是否可以跳过测试此参数值所需的代码:

  1. if (!point) {...
  2. if (!(point instanceof Point)) {...

谢谢

class Point{
  /**
   * Add x/y values to the Point object
   *
   * @param {!Point} point The x/y values to add.
   *
   */
  add(point) {
    this.x += point.x;
    this.y += point.y;
  }
}

【问题讨论】:

    标签: javascript google-closure-compiler jsdoc3


    【解决方案1】:

    是的,只要正在编译调用该方法的任何代码。

    如果你有代码调用了没有被编译的方法,那么编译器就不能保证传递的类型。例如,如果您使一个库由非编译代码调用。在这种情况下,您可能正在导出 add 方法来制作它 外部非编译代码可访问。

    如果您不导出 add 方法,则只有编译后的代码可以访问它。

    请参阅 Michael Bolin 的 Closure The Definitive Guide 第 408 页上的 Checks Provided by the Compiler 部分。您需要为编译器指定某些标志,例如:

    --jscomp_error=checkTypes
    --warning_level=VERBOSE
    

    【讨论】:

      猜你喜欢
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多