網頁制作poluoluo文章簡介:JavaScript 關於引用那點事.
<script language="javascript" type="text/javascript">
//創建變量testArray 並引用數組 ["1","2","3"]
var testArray = ["1","2","3"];
//引用數組 ,此時 變量 linkOne linkTwo testArray 均引用數組 ["1","2","3"]
var linkOne =linkTwo =testArray;
//修改testArray 引用數組 ["4","5","6"],此時 linkOne linkTwo 仍引用數組 ["1","2","3"]
testArray = ["4","5","6"]
//這裡將返回 2
alert(linkTwo[1]);
//創建變量testObj 並引用對象 {one:"1",tow:"2",three:"3"}
var testObj = {one:"1",tow:"2",three:"3"}
//引用對象, 此時objLinkOne 引用 對象 {one:"1",tow:"2",three:"3"}
var objLinkOne = testObj
//修改對象
testObj.tow="133"
//testObj引用新對象{one:"4",tow:"5",three:"6"} objLinkOne仍引用原對象
testObj = {one:"4",tow:"5",three:"6"}
//這裡將返回 133
alert(objLinkOne.tow);
//strTest 引用字符串 im
var strTest = "im";
//strTestNew 引用字符串 im
var strTestNew = strTest;
//strTestNew 引用新的字符串 im new str
strTestNew += " new str";
//這裡將返回 im
alert(strTest);
</script>