简体   繁体   English

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

[英]How to Implement a Callback in JavaScript return Value with a Dictionary

I am trying to implement a JS Dictionary which has a key and value, where the value calls a callback function for validation.我正在尝试实现一个具有键和值的 JS 字典,其中值调用回调 function 进行验证。

For example I have tried defining an animalsDict dictionary.例如,我尝试定义一个 animalsDict 字典。 When I try calling the "Dog" key to call the value validateDogSound() callback function, the bootbox.alert doesn't show.当我尝试调用“Dog”键来调用值 validateDogSound() 回调 function 时,bootbox.alert 不显示。

What am I doing wrong?我究竟做错了什么? Is this how we do callback functions in a dictionary?这就是我们在字典中做回调函数的方式吗? Any tips or help?任何提示或帮助? Thank you谢谢

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

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

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

You need to call the function:您需要调用function:

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

Also change the object to还将 object 更改为

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

this way这边走


// "functions" first !

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


var animalsDict = {
    Dog: validateDogSound ,  // no parenthesis
  //Cat: validateCatSound , 
  //Bird: ... etc
}
console.log('testDog', animalsDict["Dog"]() );  // with parenthesis

According to the documentation , you need to load the javascript libraries in the following order.根据文档,您需要按以下顺序加载 javascript 库。

Since Bootbox is a wrapper around Bootstrap's modal functionality, you need to include the libraries in order:由于 Bootbox 是 Bootstrap 模式功能的包装器,因此您需要按顺序包含库:

 jQuery Popper.js Bootstrap Bootbox Bootbox Locales (optional - omit if you only need the default English locale)

Try adding these headers in the same order or use the following.尝试以相同的顺序添加这些标题或使用以下内容。 I added the following in the header and looks like it worked.我在 header 中添加了以下内容,看起来它有效。 You will know better.你会更清楚。 Please note I have not verified the urls, this is just for demonstration.请注意我没有验证网址,这只是为了演示。 It is better if you have local copies of these on your server and are appropriately referenced in the head.如果您的服务器上有这些的本地副本并在头部适当地引用,那就更好了。

<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.5.2/bootbox.min.js"></script>
</head>
<body>
<script>
var animalsDict = {
    Dog: validateDogSound(),
  //Cat: validateCatSound(), 
  //Bird: ... etc
}

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

console.log('testDog', animalsDict["Dog"]);
</script>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM