The behavior of `$()` handling a jQuery object
I got a question here. (I've searched on StackOverflow, but found nothing helpful.)
I noticed this paragraph in
jQuery's API doc :
Cloning jQuery Objects
When a jQuery object is passed to the `$()` function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.
That means, if `$elem` is already a wrapped jQuery object, `$($elem)` will still unwrap-and-wrap the element(s) in `$elem` to create a new jQuery object, rather than returns `$elem` directly.
Initially, my workmates and I supposed that jQuery needed to re-query the selector of `$elem` to keep up with the modified DOM. Then we tried these codes:
- $wrapper.append('<p>test</p>')
- var $elem = $('p')
- $wrapper.append('<p>test</p>')
- var $elem = $($elem)
But we found `$elem2` contained the same DOM element(s) as `$elem` did. That proved `$($elem)` didn't do any "re-query" thing.
So finally, we're confused why not return `$elem` directly when it's already a jQuery object. Any other purpose for creating (cloning) a new object?