Array.size()vs Array.length

本文翻译自:Array.size() vs Array.length

What is the difference between the two? 两者有什么区别?

So I know that array.size() is a function while array.length is a property. 所以我知道array.size()是一个函数,而array.length是一个属性。 Is there a usecase for using one over the other? 是否有用于使用其中一个的用例? Is one more efficient? 一个更有效率? (I would imagine .length to be significantly faster as it is a property rather then a method call?) Why would one ever use the slower option? (我认为.length会明显更快,因为它是一个属性而不是方法调用?)为什么一个人会使用较慢的选项? Are there some browsers that are incompatible with one or the other? 是否有某些浏览器与其中一个不兼容?

  var x = [];
  console.log(x.size());
  console.log(x.length);
  console.log(x.size()==x.length);
  x =[1,2,3];
  console.log(x.size());
  console.log(x.length);
  console.log(x.size()==x.length);

Will print: 将打印:

  0, 0, true
  3, 3, true

#1楼

参考:https://stackoom.com/question/xakD/Array-size-vs-Array-length


#2楼

.size() is not a native JS function of Array (at least not in any browser that I know of). .size() 不是 Array的本机JS函数(至少在我知道的任何浏览器中都没有)。

.length should be used. 应该使用.length


If 如果

.size() does work on your page, make sure you do not have any extra libraries included like prototype that is mucking with the Array prototype. .size() 在页面上工作,请确保你没有像包含任何额外的库的原型与该碴Array原型。

or 要么

There might be some plugin on your browser that is mucking with the Array prototype. 您的浏览器上可能有一些插件正在使用Array原型。


#3楼

Array.size() is not a valid method Array.size()不是有效的方法

Always use the length property 始终使用length属性

There is a library or script adding the size method to the array prototype since this is not a native array method. 有一个库或脚本将size方法添加到数组原型中,因为这不是本机数组方法。 This is commonly done to add support for a custom getter. 这通常用于添加对自定义getter的支持。 An example of using this would be when you want to get the size in memory of an array (which is the only thing I can think of that would be useful for this name). 使用它的一个例子是当你想获得数组内存的大小时(这是我能想到的唯一对这个名称有用的东西)。

Underscore.js unfortunately defines a size method which actually returns the length of an object or array. 不幸的是,Underscore.js定义了一个实际返回对象或数组长度的size方法。 Since unfortunately the length property of a function is defined as the number of named arguments the function declares they had to use an alternative and size was chosen (count would have been a better choice). 不幸的是,函数的长度属性被定义为函数声明的命名参数的数量,他们必须使用替代方法并选择大小(计数将是更好的选择)。


#4楼

.size() is jQuery's, much probably you're either confusing with or took from someone else who had imported the jQuery library to his project. .size()是jQuery的,很可能你要么是混淆了,要么是从将jQuery库导入到他的项目中的其他人那里拿走的。

If you'd have jQuery imported and you'd write like $(array).size() , it would return the array length. 如果您已经导入了jQuery并且您编写了类似$(array).size() ,它将返回数组长度。


#5楼

The .size() function is available in Jquery and many other libraries. .size()函数在Jquery和许多其他库中可用。

The .length property works only when the index is an integer. .length属性仅在索引为整数时有效。

The length property will work with this type of array: length属性与这种类型的阵列的工作:

var nums = new Array();
nums[0] = 1; 
nums[1] = 2;
print(nums.length); // displays 2

The length property won't work with this type of array: length属性适用于此类型的数组:

var pbook = new Array(); 
pbook["David"] = 1; 
pbook["Jennifer"] = 2;
print(pbook.length); // displays 0

So in your case you should be using the .length property. 所以在你的情况下你应该使用.length属性。


#6楼

array.length isn't necessarily the number of items in the array: array.length不一定是数组中的项数:

var a = ['car1', 'car2', 'car3'];
a[100] = 'car100';
a.length; // 101

The length of the array is one more than the highest index. 数组的长度比最高索引多一个。

As stated before Array.size() is not a valid method. 如前所述, Array.size()不是有效的方法。

More information 更多信息

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值