如何设置闭包编译器注释/ externs以正确编译以下内容?
var ac, volumeNode;
var load_sound = true;
var contextClass = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
if (contextClass) {
ac = new contextClass();
volumeNode = ac.createGain();
volumeNode.connect(ac.destination);
} else {
load_sound = false;
}
目前它重命名createGain,destination,window.AudioContext等。
我知道我可以使用["正确"]语法来访问所有内容,但这会在我的代码中造成很多混乱。有更简单的方法吗?
答案 0 :(得分:1)
更新: w3c_audio.js
外部已移至编译器的默认外部。在下一个版本(版本高于20150315)中,将自动包含外部版本。
如果您需要手动引用外部,您可以在主外部文件夹中找到它们:https://github.com/google/closure-compiler/blob/master/externs/browser/w3c_audio.js
答案 1 :(得分:0)
对于那些来这个页面寻找工作示例的人,我已经设置了 jsFiddle 设置,其代码可以正常运行并在GCC中编译:
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @externs_url https://raw.githubusercontent.com/google/closure-compiler/master/contrib/externs/jquery-1.9.js
// @externs_url https://raw.githubusercontent.com/tambien/AudioJS/master/compiler/externs/w3c_audio.js
// ==/ClosureCompiler==
var DialPadContext;
var Oscillator1;
function Start() {
$('.Key').mousedown(function () {
PlayTone($(this).text());
});
$('.Key').mouseup(function () {
if (Oscillator1 && Oscillator1.disconnect) {
Oscillator1.disconnect();
}
});
DialPadContext = new window.AudioContext();
}
function PlayTone(TheKeyValue) {
var TonesFrequencies = {
'0': [941.0, 1336.0],
'1': [697.0, 1209.0],
'2': [697.0, 1336.0],
'3': [697.0, 1477.0],
'4': [770.0, 1209.0],
'5': [770.0, 1336.0],
'6': [770.0, 1477.0],
'7': [852.0, 1209.0],
'8': [852.0, 1336.0],
'9': [852.0, 1477.0],
'*': [941.0, 1209.0],
'#': [941.0, 1477.0]
}
Oscillator1 = DialPadContext.createOscillator();
Oscillator1.frequency.value = TonesFrequencies[TheKeyValue][0];
var gainNode = DialPadContext['createGain']();
Oscillator1.connect(gainNode, 0, 0);
gainNode.connect(DialPadContext.destination);
gainNode.gain.value = .1;
Oscillator1.start(0);
}
$(Start);