javascript - 關於js可變參數的一個取值疑問
天蓬老师
天蓬老师 2017-06-26 10:51:16
0
2
797

看到這麼一篇文章: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];
不是用點而是用的[],陣列?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(2)
ringa_lee

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

阿神

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板