作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不是很了解函数闭包和匿名函数。我想做的是创建一个函数,该函数根据掷骰子随机运行输入的函数:
repeat(1,6,foobar());
function repeat(numDie, dieType, func){
var total = 0;
for (var i=0; i < numDie; i++){
var dieRoll = Math.floor(Math.random()*dieType)+1;
total += dieRoll;
}
for (var x=0; x < total; x++){
func();
}
}
我到底做错了什么?我是否必须将函数存储在变量中才能使用它?
最佳答案
通过编写 foobar()
,您正在调用 foobar
并传递其返回值。
删除括号以传递函数而不是调用它。
关于javascript - 如何将函数作为参数传递以运行多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6193654/
我是一名优秀的程序员,十分优秀!