【问题标题】:Iteration inside function parenthesis函数括号内的迭代
【发布时间】:2014-11-03 18:58:01
【问题描述】:

此代码可以正常工作。

<script>
    function myfunction(a,b,c,d,e,f,g,h,i,j){
        alert(a);
        alert(b);
        alert(c);
        alert(d);
    }
</script>
<html>
<body>
<p>Hello world</p>
<!-- the arguments in the function are hardcoded -->
<button onclick="javascript: myfunction(1,2,3,4,5,6,7,8,9,10)">Submit</button>
</body>
</html>

是否可以这样做:

<button onclick="javascript: myfunction(iterate 10 times)">Submit</button>

【问题讨论】:

  • “迭代 10 次”是什么意思?
  • for (var i = 0; i
  • 您的问题不清楚。您的意思是运行函数 10 次,还是迭代传递给参数列表可变的函数的每个参数?
  • -1 请你的问题不是很清楚,你对函数参数有什么期望?谓词?参数列表?
  • 我只是想知道是否可以在函数的括号内进行迭代。不多也不少。

标签: javascript function iteration


【解决方案1】:

在js中

function forFn(from, to){
    var a=[];
    for(var i = from; i <= to; i++){
        a.push(i);
    } 
    return a;
}

在html中

<button onclick="javascript: myfunction.apply(null, forFn(1,10))">Submit</button>

【讨论】:

    【解决方案2】:

    您可以创建一个迭代参数时间的函数。

    myfunction(5) 会重复 5 次,myfunction(10) 会重复 10 次。

    function myfunction(a){
        for (i = 1; i <= a; i++) { 
            alert(i);
        }
    }
    
    myfunction(5);
    

    http://jsfiddle.net/L2sp918y/1/

    【讨论】:

    • 这将在函数内部进行迭代,但不在函数括号中。
    • 这也是我要给出的答案。我认为你需要改进你的问题,因为你真的不清楚你在追求什么。
    • 不幸的是,这不是我当时需要的答案。用户 Cevek 理解它,所以它已经足够清楚了。
    猜你喜欢
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    相关资源
    最近更新 更多