2012-02-14 71 views
0

我只是不明白高级模式下的google闭包编译器和各自的extern。这个函数的外部功能是什么

具体问题:有谁能告诉我如何让CC保持高级模式,不必重命名这个函数,因为我需要从我的HTML(<a href="javascript:searchAddress();">)中调用它。

function searchAddress() { 

    geocoder = new google.maps.Geocoder(); 
    var useraddress = $('#where').val(); 

    if (geocoder && useraddress) { 
     geocoder.geocode({'address': useraddress, 'region': region}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       myPosition = results[0].geometry.location; 
       myAccuracy = 150; 
       echoAddress(results[0].formatted_address); 
      } 
     }); 
    } 
} 

我想我明白我需要写一个外部文件,因为该功能正在从外部代码调用,通过类似

window['searchAddress'] = searchAddress 

function searchAddress() {} 

但这些都不和其他几个尝试工作。 CC编译没有错误,但浏览器抱怨

Uncaught exception: ReferenceError: Undefined variable: searchAddress

searchAddress()已经由CC删除,是不是在我min.js函数名了。感谢您的任何提示。解释赞赏,谢谢。

+0

这种情况下不需要extern。这应该是足够的: function searchAddress(){ alert(“me”); } window ['searchAddress'] = searchAddress;我为此编译的代码如下: window.searchAddress = function(){alert(“me”)}; – John 2012-02-14 17:55:36

+0

我想你会把外汇与出口混为一谈。 Externs阻止重命名。导出防止死代码删除。 “窗口[...]”技巧是导出函数的正确方法。 – 2012-02-16 02:25:20

回答

0

你并不需要创建一个外部,你要导出的函数:

http://code.google.com/closure/compiler/docs/api-tutorial3.html

这个添加到代码(不是一个外部文件):

窗口['searchAddress '] = searchAddress

+0

也感谢上面评论的人。我确实混淆了extern和exports,并没有得到它,我不得不直接在我的.js文件中写出我的出口......谢谢 – Hein 2012-02-16 08:00:56