属性比方法快吗?需要理由 [英] Property is faster than method? Need reason for it
问题描述
当我用这个谷歌搜索这个问题时,一个人给出的答案是属性比方法快,并给出了size()
和length
的一个示例.
As I google this question so one person give answer that property is faster than method and give one example of size()
and length
.
他说长度比大小快,因为长度是属性.你能告诉我这是正确的吗?或者,如果您能举个例子,那对我来说将是很棒的.
He said length is faster than size because length is property. Can you please let me know is it correct ? Or If you will give example then it will be great for me.
推荐答案
size
内部调用该长度
//http://code.jquery.com/jquery-latest.js
// The number of elements contained in the matched element set
size: function() {
return this.length;
},
因此,如果您使用的是长度,则可以避免一个额外的方法调用. Jquery文档说:
So if you are using length then you are avoiding one extra method call. The Jquery docs says:
.size()方法在功能上等效于.length属性; 但是,首选 .length属性,因为它没有 函数调用的开销.
The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.
这篇关于属性比方法快吗?需要理由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!