搜尋

首頁  >  問答  >  主體

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 天前805

全部回覆(2)我來回復

  • ringa_lee

    ringa_lee2017-06-26 10:53:16

    .運算子存取屬性時,屬性名是寫定的,不能夠根據表達式動態的生成屬性名的.
    []運算子存取屬性時,可以根據[的表達式動態產生屬性名,得到屬性名稱這個字串.

    回覆
    0
  • 阿神

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

    style.arguments[1] 相當於 style['arguments'][1]

    回覆
    0
  • 取消回覆