var x = [10, 9, 8]; var y = x; x[0] = 2; alert("The value of y"s first element is: " + y[0]);
你猜结果是多少呢? 再看这个:
// Declare a reference type (array) var refType = ["first ", " second", " third"];
// Declare a primitive type (number) var primType = 10;
// Declare a function taking two arguments, which it will modify function modifyValues(ref, prim) { ref[0] = "changed"; // modify the first argument, an array prim = prim - 8; // modify the second, a number }
// Invoke the function modifyValues(refType, primType); // Print the value of the reference type document.writeln("The value of refType is: ", refType+"< >"); // Print the value of the primitive type document.writeln("The value of primType is: ", primType);
测试时发现一个问题:
var arr=[]; arr["a"]=1; arr["b"]=2; alert(arr.length);
var arr=[]; arr=[1,2,3]; arr.test="test"; alert(arr); alert(arr[1]); alert(arr["test"]);
var arr={}; arr=[2,3,4]; alert(arr[0]); var arr={};和var arr=[]都可以写成var arr=function(){}; 从上面的代码可以看出,下标数组跟联合数组分别独立,下标只作用于下标数组,不能访问联合数组,相应的,联合数组不对length属性产生影响。联合数组的标签就是属性,但是下标数组的下标并不是属性,arr[0]!=arr.0,访问arr.0会出错。