我一直在阅读Eloquent Javascript书,但是我只是开始弄弄他们的一些示例。当您为函数输入不同的值时,我试图使一个函数具有多个不同的状态。
const missileLaunch = function() {
if (missileLaunch(SafeMode)) {
console.log("The missiles are not ready to launch")
} else if (missileLaunch(Loaded)) {
console.log("The missiles have been targeted")
} else if (missileLaunch(UraniumNotEnriched)) {
console.log("The uranium has ot been enriched enough")
}
};
missileLaunch(SafeMode);
它告诉我该函数的SafeMode状态未定义。
missileLaunch(SafeMode);
答案 0 :(得分:0)
在定义函数时,您缺少SafeMode,Loaded和UraniumNotEnriched函数参数。您应该使用SafeMode,Loaded和UraniumNotEnriched作为参数,因为上述代码段使用这些参数进行条件检查。
以下应起作用:
const missileLaunch = function(SafeMode, Loaded, UraniumNotEnriched )
答案 1 :(得分:0)
您的代码有问题
const missileLaunch = function() {
if (missileLaunch(SafeMode)) {
console.log("The missiles are not ready to launch")
} else if (missileLaunch(Loaded)) {
console.log("The missiles have been targeted")
} else if (missileLaunch(UraniumNotEnriched)) {
console.log("The uranium has ot been enriched enough")
}
};
missileLaunch(SafeMode);
第一个问题,您正在调用带有参数的函数missileLaunch
,但是在定义missileLaunch
时,它是没有参数的函数。
因此,要解决第一个问题,可以给它一个如下所示的参数
const missileLaunch = function(parameter) {
...
};
第二种可能性是您定义了全局变量SafeMode
,但是变量的值为undefined
。因此,您可以在调用console.log(SafeMode)
之前先进行missileLaunch
,这是调试步骤,如下所示:
console.log('SafeMode value is: ', SafeMode);
missileLaunch(SafeMode)