如何使用字典在JavaScript返回值中实现回调

如何使用字典在JavaScript返回值中实现回调,javascript,dictionary,callback,alert,bootbox,Javascript,Dictionary,Callback,Alert,Bootbox,我试图实现一个JS字典,它有一个键和一个值,该值调用一个回调函数进行验证 例如,我试着定义一本动物词典。 当我尝试调用“Dog”键来调用validateDogSound()回调函数时,bootbox.alert不会显示 我做错了什么?这就是我们在字典中使用回调函数的方式吗?有什么建议或帮助吗?多谢各位 var animalsDict = { Dog: validateDogSound(), //Cat: validateCatSound(), //Bird: ... etc }

我试图实现一个JS字典,它有一个键和一个值,该值调用一个回调函数进行验证

例如,我试着定义一本动物词典。 当我尝试调用“Dog”键来调用validateDogSound()回调函数时,bootbox.alert不会显示

我做错了什么?这就是我们在字典中使用回调函数的方式吗?有什么建议或帮助吗?多谢各位

var animalsDict = {
    Dog: validateDogSound(),
  //Cat: validateCatSound(), 
  //Bird: ... etc
}

function validateDogSound(){
 bootbox.alert("dogs bark!");  
 return false;
}

console.log('testDog', animalsDict["Dog"]);

您需要调用函数:

console.log('testDog', animalsDict["Dog"]());
//                                       ^
同时将对象更改为

var animalsDict = {
    Dog: validateDogSound,
  //Cat: validateCatSound, 
  //Bird: ... etc
}

您需要调用函数:

console.log('testDog', animalsDict["Dog"]());
//                                       ^
同时将对象更改为

var animalsDict = {
    Dog: validateDogSound,
  //Cat: validateCatSound, 
  //Bird: ... etc
}
这边


//“功能”第一!
var validateDogSound=函数()
{
引导箱。警报(“狗吠!”);
返回false;
}
变量animalsDict={
Dog:validateDogSound,//无括号
//Cat:validateCatSound,
//伯德:……等等
}
console.log('testDog',animalsDict[“Dog”]());//带括号
这样


//“功能”第一!
var validateDogSound=函数()
{
引导箱。警报(“狗吠!”);
返回false;
}
变量animalsDict={
Dog:validateDogSound,//无括号
//Cat:validateCatSound,
//伯德:……等等
}
console.log('testDog',animalsDict[“Dog”]());//带括号
根据,您需要按照以下顺序加载javascript库

由于Bootbox是Bootstrap模式功能的包装器,因此您需要按照以下顺序包括库:

jQuery
Popper.js
Bootstrap
Bootbox
Bootbox Locales (optional - omit if you only need the default English locale)
尝试按相同顺序添加这些标题,或使用以下命令。我在标题中添加了以下内容,看起来效果不错。你会更清楚的请注意,我没有验证URL,这只是为了演示。如果您的服务器上有这些URL的本地副本,并且在头部有适当的引用,则更好


变量animalsDict={
Dog:validateDogSound(),
//Cat:validateCatSound(),
//伯德:……等等
}
函数validateDogSound(){
引导箱。警报(“狗吠!”);
返回false;
}
log('testDog',animalsDict[“Dog”]);
根据,您需要按照以下顺序加载javascript库

由于Bootbox是Bootstrap模式功能的包装器,因此您需要按照以下顺序包括库:

jQuery
Popper.js
Bootstrap
Bootbox
Bootbox Locales (optional - omit if you only need the default English locale)
尝试按相同顺序添加这些标题,或使用以下命令。我在标题中添加了以下内容,看起来效果不错。你会更清楚的请注意,我没有验证URL,这只是为了演示。如果您的服务器上有这些URL的本地副本,并且在头部有适当的引用,则更好


变量animalsDict={
Dog:validateDogSound(),
//Cat:validateCatSound(),
//伯德:……等等
}
函数validateDogSound(){
引导箱。警报(“狗吠!”);
返回false;
}
log('testDog',animalsDict[“Dog”]);

如果你想引用函数,你需要像
var animalsDict={Dog:validateDogSound}
这样做,然后当你想调用它时,你可以做
animalsDict[“Dog”]()
如果你想引用函数,你需要像
var animalsDict={Dog:validateDogSound}这样做
然后当您想调用它时,您可以执行
animalsDict[“Dog”]()
为什么要先运行?感谢您的详细介绍和评论@Nick你说得对,在这种情况下这会起作用,但是如果她使用复杂对象调用,这是唯一的方法什么是复杂对象调用?@Nick objects with methods,我还将函数更改为constWhy function first?感谢你的分解和评论@Nick你是对的,在这种情况下,这会起作用,但是如果她使用复杂对象调用,这是唯一的方法什么是复杂对象调用?@Nick objects with methods,我还将函数改为Cont建议接受这个答案而不是另一个答案,在使用函数之前,你肯定不需要声明函数(至少对于函数声明,函数表达式是另一个故事)我建议接受这个答案而不是另一个答案,在使用函数之前,您肯定不需要声明函数(至少对于函数声明,函数表达式是另一个故事)