gpt4 book ai didi

javascript - 为什么函数在全局声明变量而不是 var 时起作用

转载 作者:行者123 更新时间:2023-11-30 09:16:20 25 4
gpt4 key购买 nike

我想将属性 soundFileName 声明为 var soundFileName = 'audio/60.wav'; 这样 soundFileName 就不会全局定义,但是当我这样做时,我得到 ReferenceError: soundFileName is not defined

我将 soundFileName 的值作为参数传递给 loop(soundFileName),我认为值 'audio/60.wav' 应该可以顺利通过。

我怀疑这与范围或嵌套有关,但我不确定如何解决该问题。当我使用不带 var 的 soundFileName = 'audio/60.wav'; 时,代码确实有效。

我错过了什么?谢谢!

编辑:代码现在可以工作并更新了!

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script src="js/howler.core.js"></script>
<link rel="stylesheet" href="css/styles.css">

</head>

<body>

<script src="timeprobabilities.js"></script>

<script>
///////////////////////////////////////////////////////////////
// MASTER START ///////////////////////////////////////////////
///////////////////////////////////////////////////////////////

// initiates fist call to all clocks to each will start and can then be called again

(function masterStart() {
setTimeout(function() {
//// LOOP SOUNDS \\\\
A();
}, 0);
}());

///////////////////////////////////////////////////////////////
// LOOPS SHARED OPTIONS ///////////////////////////////////////
///////////////////////////////////////////////////////////////

var options = {
numberOfSounds: 0,
maxNumberOfSounds: 4
};

function logNumberOfSounds() { // passing options into this before is what broke code
options.numberOfSounds++;
//console.log('Number of sounds is: ' + options.numberOfSounds + '########');
}

///////////////////////////////////////////////////////////////
// LOOP A ////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

function A() {
var optionsA = {
playDurationMin: 0,
playDurationMax: 60000,
// start time minimum and maximum
startMinA: 0,
startMaxA: 8000,
maxVolumeA: 1,
//
startMinB: 0,
startMaxB: 30000,
maxVolumeB: 1,
//
startMinC: 0,
startMaxC: 30000,
maxVolumeC: 1,
//
startMinD: 0,
startMaxD: 30000,
maxVolumeD: 1,
//
startMinE: 0,
startMaxE: 30000,
maxVolumeE: 1,
//
startMinF: 0,
startMaxF: 30000,
maxVolumeF: 1,
//
startMinG: 0,
startMaxG: 30000,
maxVolumeG: 1,
//
startMinH: 0,
startMaxH: 30000,
maxVolumeH: 1,
//
startMinI: 0,
startMaxI: 30000,
maxVolumeI: 1,
//
startMinJ: 0,
startMaxJ: 30000,
maxVolumeJ: 1,
//
startMinK: 0,
startMaxK: 30000,
maxVolumeK: 1
};

masterClock();

function masterClock() {
setTimeout(function() {
soundA(options, optionsA);
}, 10); // these need to be called with delay so they don't use the other functions' paramaters
}

function soundA() {

var soundFileName = 'audio/60.wav';
fadeIn = 8000;
fadeOut = 8000;

console.log('soundFileName in A: ' + soundFileName);

calculateStartDelay(optionsA.startMinA, optionsA.startMaxA);

function calculateStartDelay(startMin, startMax) {
startDelay = Math.floor(Math.random() * startMax) + startMin;
}

function calculatePlayDuration(playDurationMin, playDurationMax) {
playDuration = Math.floor((Math.random() * playDurationMax) + playDurationMin);
}

function executePlayTools() {
calculatePlayDuration(optionsA.playDurationMin, optionsA.playDurationMax);
loop(options, playDuration, soundFileName, fadeIn, fadeOut);
console.log('A: ////////////////////////////////// ');
masterClock();
}

setTimeout(function() {
if (probabilityValue < probabilityPointA) {
maxVolume = optionsA.maxVolumeA;
executePlayTools();
} else if (probabilityValue < probabilityPointB) {
maxVolume = optionsA.maxVolumeB;
executePlayTools();
} else if (probabilityValue < probabilityPointC) {
maxVolume = optionsA.maxVolumeC;
executePlayTools();
} else if (probabilityValue < probabilityPointD) {
maxVolume = optionsA.maxVolumeD;
executePlayTools();
} else if (probabilityValue < probabilityPointE) {
maxVolume = optionsA.maxVolumeE;
executePlayTools();
} else if (probabilityValue < probabilityPointF) {
maxVolume = optionsA.maxVolumeF;
executePlayTools();
} else if (probabilityValue < probabilityPointG) {
maxVolume = optionsA.maxVolumeG;
executePlayTools();
} else if (probabilityValue < probabilityPointH) {
maxVolume = optionsA.maxVolumeH;
executePlayTools();
} else if (probabilityValue < probabilityPointI) {
maxVolume = optionsA.maxVolumeI;
executePlayTools();
} else if (probabilityValue < probabilityPointJ) {
maxVolume = optionsA.maxVolumeJ;
executePlayTools();
} else {
maxVolume = optionsA.maxVolumeK;
console.log('Probability Else');
}
console.log('startDelay: ' + startDelay)
}, startDelay);
}
}

///////////////////////////////////////////////////////////////
// SHARED LOOP ///////////////////////////////////////////////
///////////////////////////////////////////////////////////////

function loop(options, playDuration, soundFileName, fadeIn, fadeOut) {

console.log('soundFileName in loop: ' + soundFileName);

if (options.numberOfSounds < options.maxNumberOfSounds) { //Don't create more than the max number of sounds.

var sound = getSound(soundFileName);
var id2 = sound.play();

logNumberOfSounds();

sound.volume(0); // don't think I need this since it's declared above and in getSound(), but it stops blips?
sound.fade(0, maxVolume, fadeIn, id2); // FADE IN

setTimeout(function() {
sound.fade(maxVolume, 0, fadeOut, id2); // FADE OUT
options.numberOfSounds--;

// Attempt to clean up the sound object
setTimeout(function() {
sound.stop();
sound.unload();
}, fadeOut + 1000);
}, playDuration);
}

}

// PLAYER FOR MAIN SOUND FUNCTION /////////////////////////////
function getSound(soundFileName) {
return new Howl({
src: [soundFileName],
autoplay: true,
loop: true,
volume: 0,
fade: 0 // removes the blip
});
}
</script>

<script src="js/howler.core.js"></script>
<script src="js/siriwave.js"></script>
<script src="js/player.js"></script>

</body>

</html>

最佳答案

每个函数都有自己的作用域,函数内声明的变量仅存在于该函数的作用域内

在这种情况下,soundFileName 被声明为 soundA 中的 var,因此它仅存在于 soundA 的范围内>.

您将 soundFileName 作为参数传递给 loop,然后从那里将其传递给 getSound,但是您的 function definitions for loopgetSound 不包含任何命名参数。

您需要更改 function definitionloopgetSound 包含预期的参数:

function loop(options, playDuration, soundFileName, fadeIn, fadeOut) {
...
// soundFileName is now availabie in this scope
...
}


function getSound(soundFileName) {
...
// soundFileName is now availabie in this scope
...
}

请注意,在 JavaScript 中的每个函数中(不包括 arrow functions )都有 a special arguments object包含调用函数的参数,因此由于您将 soundFileName 作为 loop 的第三个参数传递,传递的值也将在 loop 中可用> 作为 arguments[2],并且由于它作为第一个参数传递给 getSound,它也将在 getSound 中作为 可用参数[0]

关于javascript - 为什么函数在全局声明变量而不是 var 时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54849385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com