What syntax I should use to declare externs for closure compiler?

独自空忆成欢 提交于 2019-12-10 12:10:26

问题


I want to declare some externs for closure compiler but not know how to do it?

(function(window) {
window.myapi = window.myapi || {};

var myapi = window.myapi;

myapi.hello = function() {
  window.document.write('Hello');
}
}(window));

I am not sure how to do it for window.myapi, window.myapi.hello?


回答1:


Externs are valid javascript, but they are just type information. They should not contain definitions (or for functions only empty definitions).

Here's a start: How to Write Closure-compiler Extern Files Part 1

A couple of notes on your specific example:

  1. Don't use an anonymous wrapper. Type names must be global.
  2. Properties on the window object are the same as the namespace examples.
  3. Functions shouldn't have implementations

Here's a corrected example:

/** @const */
window.myapi = {};

/** @return {undefined} */
window.myapi.hello = function() {};

In Closure-compiler properties on the window (global) object are seen completely differently than global variables. If you need both, you'll have to declare everything twice.

/** @const */
var myapi = {};

/** @return {undefined} */
myapi.hello = function() {};


来源:https://stackoverflow.com/questions/20100507/what-syntax-i-should-use-to-declare-externs-for-closure-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!