JQuery Link Caching

Is it always better to use:

var $this = $(this);

Or is $ (this) cached, and so the above line is only for saving two characters?

+3
source share
2 answers

Using $(this)calls at least two (and possibly more than two) functions and allocates an object every time you use it (consuming memory, which ultimately needs to be fixed). This is additional work if you intend to reuse the same thing. I would recommend calling it once and then caching the result (for example, inside a function), rather than having a dozen lines $(this).foo(); $(this).bar();.

$ jQuery, :

var jQuery = function( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context );
}

, , - jQuery.fn.init. , , jQuery jQuery 18 . , , .: -)

. , DOM ( $(this)), jQuery.fn.init . , "" .

+3

$ jQuery ( , , ), / .

, , .

, .

0

Source: https://habr.com/ru/post/1752602/


All Articles