Array.size()VS Array.length [英] Array.size() vs Array.length
问题描述
两者有什么区别呢?
所以我知道 array.size()
是一个函数,而 array.length
是一个属性。是否有使用一个比其他一个用例?一个更有效的(我可以想象。长度
是显著更快,因为它是一个属性而不是一个方法调用?)为什么人会使用较慢的选项?是否有某些浏览器不兼容的一个或其他?
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);
将会打印:
0,0,真
3,3,真
Array.size()不是一个有效的方法
始终使用length属性
有一个图书馆或脚本添加尺寸方法对阵列原型,因为这不是本机阵列的方法。这是常见的做添加自定义的getter支持。如果你想获得的大小的数组(这是我能想到的唯一的事情是对这个名字很有用)的内存使用此的一个例子是,
Underscore.js不幸的是定义了一个尺寸实际上返回一个对象或数组的长度
方法。由于不幸的是函数的长度属性被定义为命名参数的函数声明的数量,他们不得不使用替代和被选择的大小(计数会是一个更好的选择)。
What is the difference between the two?
So I know that array.size()
is a function while array.length
is a property. 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? 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
Array.size() is not a valid method
Always use the length property
There is a library or script adding the size method to the array prototype since this is not a native array method. This is commonly done to add support for a custom 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. 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).
这篇关于Array.size()VS Array.length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!