jQuery size() 方法与长度属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2738352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery size() method vs length attribute
提问by jantimon
Is there any difference between $(".selector").size()
and $(".selector").length
?
有什么区别$(".selector").size()
和$(".selector").length
?
回答by RoToRa
No. size()
returns length
. By using length
you only avoid one extra method call.
号size()
返回length
。通过使用,length
您只会避免一个额外的方法调用。
回答by Fabian
Length returns the same thing and is slightly faster according to the jQuery documentation.
根据 jQuery 文档,Length 返回相同的内容并且速度稍快。
Source: http://api.jquery.com/size/
回答by Jon Dean
They will both give you the same result but .length is slightly faster.
它们都会给你相同的结果,但 .length 稍微快一点。
See http://api.jquery.com/size/:
请参阅http://api.jquery.com/size/:
The .length property is a slightly faster way to get this information.
.length 属性是获取此信息的稍微快一点的方法。
回答by doodlemoonch
回答by Ben Roberts
Yes! There is now a very significant difference. .size()
is deprecated.Always use .length
instead.
是的!现在有一个非常显着的差异。.size()
已弃用。总是使用.length
代替。
回答by Jaymz
.size() is a Method call, which returns the length property. So you either call the method to return the property, or you retrieve the property directly.
.size() 是一个方法调用,它返回长度属性。因此,您要么调用该方法返回该属性,要么直接检索该属性。
The method (.size()) is probably the one you should be using, as it was most likely implemented to abstract away from the possibility of the length property being changed.
方法 (.size()) 可能是您应该使用的方法,因为它最有可能被实现以抽象出长度属性被更改的可能性。
回答by Sourav Basak
jQuery .size() and .length both return the number of elements in the jQuery object.
jQuery .size() 和 .length 都返回 jQuery 对象中元素的数量。
Size() and length in jQuery both returns the number of element in an object but length is faster than the size because length is a property and size is a method and length property does not have the overhead of a function call.
jQuery 中的 size() 和 length 都返回对象中元素的数量,但 length 比 size 快,因为 length 是一个属性,size 是一个方法,并且 length 属性没有函数调用的开销。
source: http://www.namasteui.com/difference-between-size-and-length-of-jquery/
来源:http: //www.namasteui.com/difference-between-size-and-length-of-jquery/
回答by Ramesh kumar
JQuery size()is a method & lengthis property and property is faster than method because size()internally calls length. so better to call length directly.
JQuery size()是一个方法,长度是属性,属性比方法快,因为size() 在内部调用 length。所以最好直接调用 length 。
回答by Vipul Jethva
If you will read length
property then only time required to access an object property will be needed.
如果您将读取length
属性,则只需要访问对象属性所需的时间。
However if you will call size()
then first of all a function will be called, this function will read length
property internally and then return that value to the caller.
但是,如果您将调用,size()
那么首先将调用一个函数,该函数将在length
内部读取属性,然后将该值返回给调用者。
You can clearly see that you are doing the same thing in both cases. But if you call the function then it will include time for calling a function + returning that value too..
您可以清楚地看到,您在两种情况下都在做同样的事情。但是,如果您调用该函数,那么它将包括调用函数+ 返回该值的时间。