Bug Tracker

Changes between Version 1 and Version 2 of Ticket #9525, comment 8


Ignore:
Timestamp:
Jun 9, 2011, 12:34:16 PM (10 years ago)
Author:
shadedecho
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #9525, comment 8

    v1 v2  
    55You'll see that your suggested snippet is in spirit the same thing as I already explored.
    66
    7 It works fine for the case where you only ever need to deal with one such single-element-wrapper-instance at a time, because that global $() instance can just be re-used over and over.
     7It works fine for the case where you only ever need to deal with one such single-element-wrapper-instance at a time, because that global `$()` instance can just be re-used over and over.
    88
    9 But, the major caveat comes if more than one such wrapper needs to be used. For instance, say inside an .each() loop, I need to wrap $(this) AND $(this.parentNode), and I need to use the two together at the same time. In the case of a solution where a global "flyweight" collection is just re-used, this falls apart completely, because they trample on each other.
     9But, the major caveat comes if more than one such wrapper needs to be used. For instance, say inside an .each() loop, I need to wrap `$(this)` AND `$(this.parentNode)`, and I need to use the two together at the same time. In the case of a solution where a global "flyweight" collection is just re-used, this falls apart completely, because they trample on each other.
    1010
    11 So, in that case, being able to do $_(this) and $_(this.parentNode), or whatever, is strongly desirable. The awkward (and non-performant) workaround options are:
     11So, in that case, being able to do `$_(this)` and `$_(this.parentNode)`, or whatever, is strongly desirable. The awkward (and non-performant) workaround options are:
    1212
    13 1. use `$this[0] = this` to speed that operation up, but use the slower $(this.parentNode) for the other(s).
     131. use `$this[0] = this` to speed that operation up, but use the slower `$(this.parentNode)` for the other(s).
    1414
    15152. only use one wrapper at a time, swapping back and forth as necessary, like:
    1616
     17{{{
    1718$elem[0] = this;
    1819var rel = $elem.attr("rel");
     
    2223$elem[0] = this;
    2324$elem.attr("rel", parent_rel);
     25}}}
    2426
    25 And btw, I know my example is a little contrived, as wrapping `this.parentNode` is silly compared to just doing $this.parent(). But the spirit of this is that I have two different nodes that I need to access at the same time, and I would ideally like each of them to have the handy jquery wrappers around them. But neither of them am I using as a collection where I need the overhead of stack management.
     27And btw, I know my example is a little contrived, as wrapping `this.parentNode` is silly compared to just doing `$this.parent()`. But the spirit of this is that I have two different nodes that I need to access at the same time, and I would ideally like each of them to have the handy jquery wrappers around them. But neither of them am I using as a collection where I need the overhead of stack management.
    2628
    27 $(A) and $(B) works fine for that purpose, except that they're much slower, compared to if I happen to already have two unused/idle collections sitting around, like $a and $b, that I can just do the quick assign like `$a[0] = A` and `$b[0] = B`.
     29`$(A)` and `$(B)` works fine for that purpose, except that they're much slower, compared to if I happen to already have two unused/idle collections sitting around, like `$a` and `$b`, that I can just do the quick assign like `$a[0] = A` and `$b[0] = B`.
    2830
    2931So, ideally, what I'd like is a quick way to get a simple single-element collection for any object (as many as I need at a time), without the performance overhead of doing the collection/stack-management stuff.