0

I am trying to annotate my Javascript properly in order to avoid Google Closure messing up with my variables.

I am wondering if it is possible to strongly type an anonymous function to make sure the parameters of the function (which are externs in my case) are not renamed.

Here is an illustration

/** externs.js where I define my externs */
/** @interface a Json object returned by the server */
function MyServerResult() {}
/** @type {boolean} */
MyServerResult.prototype.error;

and in another file compiled with externs.js and jquery.js as externs.

$.get("url.php", function(data) {alert(data.error;}, "json");

I am trying to make sure the anonymous function function(data) has @type {function(MyServerResult)} so error is not renamed.

Can I do that directly or do I need to create a seperate function (that will probably be inlined by the compiler...)?

Mad Echet
  • 3,723
  • 7
  • 28
  • 44

1 Answers1

2

I believe you should be able to do:

/**
 * @param {MyServerResult} data
 */
var callback = function(data) {
  alert(data.error);
};
$.get("url.php", callback, "json");
bolinfest
  • 3,710
  • 2
  • 27
  • 40
  • Thanks, sounds very rational. I will try this out. – Mad Echet Apr 26 '12 at 09:11
  • After testing, it does work in a sense that it prevents from renaming the members it is the most important. However, it generates a warning because data does not have the expected type, is there a way to remove this warning by some kind of 'casting'? – Mad Echet Apr 27 '12 at 08:13
  • Thanks, it works. I did not know about the parenthesis around the variable. Your second version is the safest I guess. – Mad Echet Apr 27 '12 at 18:16
  • Cool, removed the first version and left it with the second. – bolinfest Apr 27 '12 at 19:02