search

Home  >  Q&A  >  body text

javascript - A question about the value of js variable parameters

Saw such an article: http://www.tuicool.com/articl...
Code snippet:

<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>

Example 2 has this line: return arguments[0].style[arguments[1]];
Why is it not written like this?
return arguments[0].style.arguments[1];
Instead of using dots, use [], array?

天蓬老师天蓬老师2820 days ago796

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-06-26 10:53:16

    . When the operator accesses an attribute, the attribute name is written, and cannot dynamically generate the attribute name based on the expression. When the
    [] operator accesses the attribute, can be based on [] The expression dynamically generates the attribute name and gets the attribute name string .

    reply
    0
  • 阿神

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

    style.arguments[1] is equivalent to style['arguments'][1]

    reply
    0
  • Cancelreply