I'm trying to get the Google Closure Compiler to not rename objects when passed as settings or data to a function. By looking at the annotations present in jQuery, I thought this would work:
/** @param {Object.<string,*>} data */
window.hello = function(data) {
alert(data.hello);
};
hello({ hello: "World" });
However, it ends up like this:
window.a = function(b) {
alert(b.a)
};
hello({a:"World"});
The ajax
function found here has this annotation and it appears to work. So, why won't this? If data is the return value from an external source or a settings object I'd like to be able to tell the compiler to not touch it, using the this["escape"]
trick is to intrusive for something like this in my opinion.
Here's a better example
function ajax(success) {
// do AJAX call
$.ajax({ success: success });
}
ajax(function(data) {
alert(data.Success);
});
Output:
$.b({c:function(a){alert(a.a)}});
success
has been renamed to c
and Success
(with a capital S) has been renamed to a
.
I now compile the same code with the jQuery 1.6 externs file and get the following output:
$.ajax({success:function(a){alert(a.a)}});
It also produces a warning that the property Success
is not defined, as I would expect, but it cannot rename Success
to simply a
, that will still break my code. I look at the annotation present for the ajax
and I find this type expression {Object.<string,*>=}
, I annotate my code accordingly, and recompile. Still not working...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…