我应该使用什么语法来声明闭包编译器的外部变量? [英] What syntax I should use to declare externs for closure compiler?
问题描述
我想为闭包编译器声明一些extern,但不知道该怎么做?
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));
我不确定如何对window.myapi,window.myapi.hello做到这一点?
I am not sure how to do it for window.myapi, window.myapi.hello?
推荐答案
外部内容是有效的JavaScript,但它们只是类型信息.它们不应包含定义(或对于函数,只能包含空定义).
Externs are valid javascript, but they are just type information. They should not contain definitions (or for functions only empty definitions).
这是一个开始:关于您的特定示例的几点说明:
A couple of notes on your specific example:
- 请勿使用匿名包装.类型名称必须是全局名称.
- 窗口对象的属性与名称空间示例相同.
- 函数不应具有实现
这是一个更正的示例:
/** @const */
window.myapi = {};
/** @return {undefined} */
window.myapi.hello = function() {};
在window
(全局)对象的Closure-compiler属性中,所看到的与全局变量完全不同.如果两者都需要,则必须两次声明所有内容.
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() {};
这篇关于我应该使用什么语法来声明闭包编译器的外部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!