javascript - google.Timer.callOnce 不满足形参 : Google Closure

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

我在使用 goog.Timer.callOnce 时收到形式参数不匹配的错误,即使我认为我已经正确声明了所有内容。

goog.Timer.callOnce(/** @type {function} */ this.doSomething,0,this); 

方法定义看起来像

/**
* @param {!goog.events.Event} e
*/
model.someModel.prototype.doSomething = function(e){
}

错误看起来像

ERROR - actual parameter 1 of goog.Timer.callOnce does not match formal parameter
==> default: [ERROR] found   : function (this:model.someModel, goog.events.Event): undefined
==> default: [ERROR] required: (function (this:model.someModel): ?|null|{handleEvent: function (): ?})
==> default: [ERROR] goog.Timer.callOnce(/** @type {function} */doSomething,0,this);

我也尝试过类型转换 /** @type {function()} */ 但即使这样也没有用

最佳答案

编译器需要一个不带参数的函数(因为 goog.Timer 不会传递任何参数),但您传递的是一个需要一个参数的函数。要么更改函数使其不带参数,要么使参数可选:

/**
 * @param {!goog.events.Event=} opt_e
 */
model.someModel.prototype.doSomething = function(opt_e) {
  if (opt_e) {
    ...
  } else {
    ...
  }
}

关于javascript - google.Timer.callOnce 不满足形参 : Google Closure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32061969/

相关文章:

javascript - 如何使用 goog.module 动态加载模块

javascript - JS : With factory functions, 你必须像在类中一样设置你的参数吗?

javascript - 带按钮的多行图像 slider

javascript - 在for循环中调用异步函数

javascript - jquery .val() 和谷歌闭包编译器

ClojureScript google 闭包代码分割部分加载

javascript - 反转元素的显示 News Feed

javascript - 是否可以让 Google Closure 编译器*不*内联某些函数?

javascript - 使用 Google Closure Compiler 缩小用 Typescript 编写的 AngularJs 应用程序

javascript - Google Closure Library 最好/更稳定的特性是什么