搜索

首页  >  问答  >  正文

javascript - 关于js可变参数的一个取值疑问

看到这么一篇文章:http://www.tuicool.com/articl...
代码片段:

<script>
function css()
{
if(arguments.length==2) //获取
{
return arguments[0].style[arguments[1]];
}
else
{
arguments[0].style[arguments[1]]=arguments[2];
}
}
window.onload=function ()
{
var op=document.getElementById('p1');
alert(css(op, 'width'));   //获取width属性值
css(op, 'background', 'green');
};
</script>
<p id="p1" style="width:200px; height:200px; background:red;"></p>

例子2这么一行:return arguments[0].style[arguments[1]];
为什么不是 这样的写法?
return arguments[0].style.arguments[1];
不是用点而是用的[],数组?

天蓬老师天蓬老师2824 天前800

全部回复(2)我来回复

  • ringa_lee

    ringa_lee2017-06-26 10:53:16

    .运算符访问属性时,属性名是写定的,不能够根据表达式动态的生成属性名的.
    []运算符访问属性时,可以根据[]里面的表达式动态生成属性名,得到属性名这个字符串.

    回复
    0
  • 阿神

    阿神2017-06-26 10:53:16

    style.arguments[1] 相当于 style['arguments'][1]

    回复
    0
  • 取消回复