我已经做了一个函数来产生一个鼓声,然后被调用4次它停止使用错误:
TypeError:null不是对象(评估'audioCtx.sampleRate')在控制台中显示.
这个功能出了什么问题?我的代码是:
drum = function(){
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var frameCount = audioCtx.sampleRate/20
var myArrayBuffer = audioCtx.createBuffer(1, frameCount, audioCtx.sampleRate);
var nowBuffering = myArrayBuffer.getChannelData(0);
for (var i = 0; i < frameCount; i++) {
nowBuffering[i] =Math.sin(i**(1/1.8)/4)
}
var source = audioCtx.createBufferSource();
source.buffer = myArrayBuffer; source.connect(audioCtx.destination);
source.start();
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于闭包的内容,我经常使用它们但是我发现了一个我不理解的案例.为什么我的函数不能访问hello变量我正在测试?它不应该通过范围变化找到它吗?我的代码:
(function($){
var hello="hello world"
$.test=function(a){
alert(hello+" 1")
a()}
})(this)
test(function(){alert(hello+" 2")})
Run Code Online (Sandbox Code Playgroud)