Comments on: Retaining a reference, the simple way https://j11y.io/snippets/retaining-a-reference-the-simple-way/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Floby https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1064 Sun, 09 Aug 2009 23:08:03 +0000 https://j11y.io/?p=972#comment-1064 what about this ?

$( '#something' ).each( function(){
  $(this).width($(this).parent().innerWidth());
});
]]>
By: Michael https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1063 Thu, 02 Jul 2009 16:04:53 +0000 https://j11y.io/?p=972#comment-1063 This is a fascinating study.

I used the “Best Practices” method today for something that looks like this:

   $('#something').each(function(){
        var something = $(this);
        something.find('input').change(function(){
            myFunction(something);
        });
    });

I am curious as to whether the $_.this would work in this sort of application. Is the current value of $._this assigned permanently to the onchange, or would it use the value of $._this at the time the element was changed?

]]>
By: James https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1062 Wed, 01 Jul 2009 06:51:26 +0000 https://j11y.io/?p=972#comment-1062 @rafael; JavaScript is single threaded… Almost everything happens synchronously, one after the other… The value of $._this will always equal the last selection you made. While this, as an idea, may be considered a bad practice in other languages, its usage in JavaScript does not present any issues. Like I’ve said, the only time you would want to avoid this is when you make a selection outside of a callback function and then you wait for that callback function to fire.

Plus, to be honest, your argument is flawed since any variable is subject to change (except getters) – not just $._this.

]]>
By: rafael bandeira https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1061 Tue, 30 Jun 2009 17:35:33 +0000 https://j11y.io/?p=972#comment-1061 this looks like bad design.
This is just the same as creating a global variable, and you know that there’s no such thing as good/safe global variable.
You’ll never be able to tell wether the $._this really contains that object at that given point, as I might change it anywhere on the code and it will reflect in any code using it.

Not a good practice at all.

]]>
By: James https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1060 Mon, 29 Jun 2009 13:48:01 +0000 https://j11y.io/?p=972#comment-1060 @Steven, you raise a good point.. Using $._this in callback functions would be a bad idea, as there’s no telling what its value will be by the time the callback fires.

I recommend only using it immediately after the selection; no functions involved. It’s not an all-round problem solver; just a convenience-feature.

]]>
By: Steven Black https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1059 Mon, 29 Jun 2009 13:29:48 +0000 https://j11y.io/?p=972#comment-1059 I could be wrong, but I suspect there may be issues here with animations — stop() exists for the same reason — and when $.ajax.async=true (which is the default).

In other words, will $._this always be what we think it is?

We know a variable assignment is completely safe in this respect.

]]>
By: James https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1058 Mon, 29 Jun 2009 06:37:24 +0000 https://j11y.io/?p=972#comment-1058 @Valentino, you could rename it to $.current or anything really…

@tfforums, Yes you could, but that’s long-winded and gets quite annoying after a while.

]]>
By: tfforums https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1057 Mon, 29 Jun 2009 02:00:02 +0000 https://j11y.io/?p=972#comment-1057 coudln’t you just do this with .each() ie:

$( ‘#something’ ).each($(this).width( $(this).parent().innerWidth() )); ??

]]>
By: Valentino https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1056 Sun, 28 Jun 2009 12:14:11 +0000 https://j11y.io/?p=972#comment-1056 wow! that’s great! I only complain about the underscore in “_this”. it’s not really jQuery-style.. 😉

]]>
By: Jeferson Koslowski https://j11y.io/snippets/retaining-a-reference-the-simple-way/#comment-1055 Sat, 27 Jun 2009 22:10:50 +0000 https://j11y.io/?p=972#comment-1055 Lol, I faced this problem yesterday and adopted the “best practice” solution you mentioned, although I didn’t liked it.

Would be great to have something like this implemented in jQuery core.

]]>