开发者

jQuery: array zero vs function get zero: [0] vs get(0)

开发者 https://www.devze.com 2023-02-09 21:25 出处:网络
Is there any reason I should use $(\'#x>div\').get(1) when I could instead just use $(\'#x>div\')[1]? Is there a d开发者_如何转开发ifference?Nope, no difference. jQuery holds all DOM nodes in an

Is there any reason I should use $('#x>div').get(1) when I could instead just use $('#x>div')[1]? Is there a d开发者_如何转开发ifference?


Nope, no difference. jQuery holds all DOM nodes in an Array.

$().get(1) === $()[1]

--jQuery source snippet--

get: function( num ) {
    return num == null ?
        // Return a 'clean' array
        this.toArray() :

        // Return just the object
        ( num < 0 ? this[ this.length + num ] : this[ num ] );
},

As you can see, .get() with no arguments will return all nodes as Array. This cannot be accomplished with brackets.


No, and performance is about the same because the creation of a jQuery object dominates array/function access time:

Browser      get Ops/sec  array Ops/sec  #tests
Chrome 9     20,555       22,671         2
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号