您现在的位置:首页 >> 前端 >> 内容

函数的参数数组arguments的应用实例

时间:2018/6/29 16:14:45 点击:

  核心提示:arguments 是一个函数的参数数组应用1:html xmlns=https://www.w3.org/1999/xhtmlheadmeta http-equiv=Content-Type con...

arguments 是一个函数的参数数组

应用1:

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Json</title>
<script>
function sum(){
    var l=arguments.length;
    var result=0;
    for(i=0;i<l;i++){
        result+=arguments[i];
    }
    return result;
}
alert(sum(1,2,3));
</script>
</head>
<body>
</body>
</html>

应用2:

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script>
function css(){
    if(arguments.length==2){
        alert(arguments[0].style[arguments[1]]);}
    else{
        //arguments[0].style.arguments[1]=arguments[2];  //错误。属性为变量的时候不能用 . 来获取属性名
        arguments[0].style[arguments[1]]=arguments[2];
    }
}
window.onload=function(){
    var oDiv=document.getElementById("p1");
    css(oDiv,'background','red');
    css(oDiv,'height');
}
</script>
<body>
<p id="p1" style="width:200px;height:200px;background:#0F6;">
</body>
</html>

应用2 的css函数可以更简单,如下:

function css( oBj,name,value){

    if(arguments.length==2){
        alert(oBj.style[name]);}
    else{
        //arguments[0].style.arguments[1]=arguments[2];  //错误。属性为变量的时候不能用 . 来获取属性名
        oBj.style[name]=value;
    }
}

作者:网络 来源:qq_3939627