关于length的疑问?

浴火_凤凰 2009-10-16 06:30:20
请看如下三段代码:
代码段1

function arr(){}
arr["a"]="aaa";
arr["b"]="bbb";
alert(typeof arr);//弹出function
alert(arr.length);//弹出0,这里是为什么?
alert(arr.a);//弹出aaa
alert(arr["b"]);//弹出bbb

代码段2

var arr=new Object();
arr["a"]="aaa";
arr["b"]="bbb";
alert(typeof arr);//弹出object
alert(arr.length);//弹出undefined,这里是为什么?
alert(arr.a);//弹出aaa
alert(arr["b"]);//弹出bbb


代码段3

function arr(){}
arr["a"]="aaa";
arr["b"]="bbb";
alert(typeof arr);//弹出object
alert(arr.length);//弹出0,这里是为什么?
alert(arr.a);//弹出aaa
alert(arr["b"]);//弹出bbb


真的很晕啊!!!
...全文
180 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2009-10-16
  • 打赏
  • 举报
回复
If you pass only a single number to the Array( ) constructor, it specifies the length of the array. Thus:

var a = new Array(10);



creates a new array with 10 undefined elements.
sohighthesky 2009-10-16
  • 打赏
  • 举报
回复

<script type="text/javascript">
var arr=new Array(2);//初始化一个长度为2的数组
alert(arr.length);//弹出2
alert(arr[0]==null); //看这里为true
arr["a"]="aaa";
arr["b"]="bbb";
arr["c"]="ccc";
alert(arr.a);//弹出aaa
alert(arr["b"]);//弹出bbb
alert(arr["c"]);//弹出ccc
alert(arr.length);//弹出2,这里是为什么?貌似没有影响到arr.length。
arr[0]="000";
arr[1]="111";
arr[2]="222";
alert(arr[0]);//弹出000 数组索引从0开始,给第一个空元素赋值
alert(arr[1]);//弹出111
alert(arr[2]);//弹出222 这里js长度为够会自动增加
alert(arr.length);//弹出3,这里是为什么?影响到arr.length了?

</script>
浴火_凤凰 2009-10-16
  • 打赏
  • 举报
回复
还有如下代码:

var arr=new Array(2);
alert(arr.length);//弹出2
arr["a"]="aaa";
arr["b"]="bbb";
arr["c"]="ccc";
alert(arr.a);//弹出aaa
alert(arr["b"]);//弹出bbb
alert(arr["c"]);//弹出ccc
alert(arr.length);//弹出2,这里是为什么?貌似没有影响到arr.length。
arr[0]="000";
arr[1]="111";
arr[2]="222";
alert(arr[0]);//弹出000
alert(arr[1]);//弹出111
alert(arr[2]);//弹出222
alert(arr.length);//弹出3,这里是为什么?影响到arr.length了?
sohighthesky 2009-10-16
  • 打赏
  • 举报
回复
Note that the arrays described here differ from the associative arrays described in Section 3.5. The regular arrays we are discussing here are indexed by nonnegative integers
sohighthesky 2009-10-16
  • 打赏
  • 举报
回复

<script type="text/javascript">
var arr=new Array();
arr["a"]="aaa";
arr["b"]="bbb";
alert(typeof arr);//弹出function ,I am sorry,but 我这里出来的是"object"
alert(arr.length);//弹出0,这里是为什么? Array只接受number类型的索引,所以是0
alert(arr.a);//弹出aaa 因为是一个对象object,所以属性可以取值
alert(arr["b"]);//弹出bbb
</script>
浴火_凤凰 2009-10-16
  • 打赏
  • 举报
回复
真的晕了啊!!!!
代码居然都贴错了!!!
哎!!!
谢谢楼上了啊!!!!
代码段3

var arr=new Array();
arr["a"]="aaa";
arr["b"]="bbb";
alert(typeof arr);//弹出function
alert(arr.length);//弹出0,这里是为什么?
alert(arr.a);//弹出aaa
alert(arr["b"]);//弹出bbb

sohighthesky 2009-10-16
  • 打赏
  • 举报
回复
length属性,参考:
Part I Chapter 8 Section 8.6

We've seen that functions can be used as data values in JavaScript programs. The typeof operator returns the string "function" when applied to a function, but functions are really a specialized kind of JavaScript object. Since functions are objects, they have properties and methods, just like the Date and RegExp objects, for example.

8.6.1. The length Property
As shown previously, within the body of a function, the length property of the arguments array specifies the number of arguments that were passed to the function. The length property of a function itself, however, has a different meaning. This read-only property returns the number of arguments that the function expects to be passedthat is, the number of parameters it declares in its parameter list. Recall that a function can be invoked with any number of arguments, which it can retrieve through the arguments array, regardless of the number of parameters it declares. The length property of the Function object specifies exactly how many declared parameters a function has. Note that unlike arguments.length, this length property is available both inside and outside of the function body.

The following code defines a function named check() that is passed the arguments array from another function. It compares the arguments.length property to the Function.length property (which it accesses as arguments.callee.length) to see if the function was passed the number of arguments it expected. If not, it throws an exception. The check() function is followed by a test function f() that demonstrates how check() can be used:

function check(args) {
var actual = args.length; // The actual number of arguments
var expected = args.callee.length; // The expected number of arguments
if (actual != expected) { // Throw an exception if they don't match
throw new Error("Wrong number of arguments: expected: " +
expected + "; actually passed " + actual);
}
}

function f(x, y, z) {
// Check that the actual # of args matches the expected # of args
// Throw an exception if they don't match
check(arguments);
// Now do the rest of the function normally
return x + y + z;
}

sohighthesky 2009-10-16
  • 打赏
  • 举报
回复
第二个object 没有length属性,

第三个跟第一个一样,是不是copy错了
sohighthesky 2009-10-16
  • 打赏
  • 举报
回复
function arr(){}
arr["a"]="aaa";
arr["b"]="bbb";
alert(typeof arr);//弹出function 指上面声明的函数
alert(arr.length);//弹出0,这里是为什么? 这个length指函数的参数长度
alert(arr["b"]);//弹出bbb

function arr(a,b) {}
alert(arr.length);//这里为2

52,798

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧