0

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?

Chameleon
  • 9,722
  • 16
  • 65
  • 127

1 Answers1

3

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() {};
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • I have some problem since syntax: var x = {}; not declare window.x and I need declare window.x = {}; – Chameleon Nov 20 '13 at 19:28
  • Problem is that I must use anonymous wrapper because I do not want pollute or be polluted global scope at all. Thank for explanation that I need twice window.namespace and namespace declaration. I prefer control over syntax so will use long syntax window but very safe. – Chameleon Nov 21 '13 at 07:20