1

I am trying to learn jquery/javascript by looking at the source code of some websites. I am a bit unsure about the syntax used there:

!function(t) {
    "use strict";

        function e() {
        var e = parseInt(t(window).scrollTop()),
            n = 10;
        e > n ? a.addClass("new-class") : (a.removeClass("new-class"), t(".sclass").removeClass("fclass"))
    }
    //...more codes...
}(jQuery),

I just don't really get what exactly is the meaning of that t there. Is it "this" or just any event object? and what does t(window) means? I thought it should be something like t.window? Since t is not a function.

Thanks! Saldtch

PeterKA
  • 24,158
  • 5
  • 26
  • 48
saldtch
  • 55
  • 3
  • 8
  • This is minified code, and it's pointless to post it in a question. If you want info on IIFEs, see here http://stackoverflow.com/questions/8228281/what-is-this-iife-construct-in-javascript – elclanrs Jul 27 '14 at 20:03
  • I think you used a `!` instead of a `(` – PeterKA Jul 27 '14 at 20:05
  • 1
    @user3558931: Minifiers will do this for you, as it's one less character than wrapping the function in parentheses to force an expression. – elclanrs Jul 27 '14 at 20:06
  • man you guys answer super super super quick! Let me try to digest! – saldtch Jul 27 '14 at 20:09

5 Answers5

3

t is a reference to the jQuery object within the scope of that function. Notice how the function is called:

!function (t) {
    // "t" is the jQuery object
}(jQuery);

The function is defined and then immediately invoked with the parameter jQuery. So when the function is invoked, that parameter being passed is stored in the variable t. You could name it anything, really:

!function (foo) {
    // "foo" is the jQuery object
}(jQuery);
David
  • 208,112
  • 36
  • 198
  • 279
0

The function is defined inline, so it doesn't have a name, and immediatley gets called with param jQuery. "t" is the formal parameter of the inline function, which gets resolved with the concrete parameter "jQuery".

Shortly, variable t refers to the jQuery object (or whatever jQuery variable holds).

Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
0

That's equivalent to:

function x(t) {
    //code in here
}

x(jQuery);

Therefore t is a local reference in the function to jQuery.

And, t(window) is equivalent to jQuery(window) ... and by extension $(window).

PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • Thanks! I think i got this, but what about that t(window)? It doesn't look like a formal syntax to me. Or this is just minified code? (Again doesn't look like something formal to me) – saldtch Jul 27 '14 at 20:12
  • Whenever you wrap an element in `jQuery( .. )` or `$( .. )` you are able to manipulate the element using `jQuery` methods. See my updated code above. Does that help? – PeterKA Jul 27 '14 at 20:15
  • Yes I got that already thanks! (the above comment was added BEFORE your answer was updated :)) – saldtch Jul 27 '14 at 20:18
  • Great! Glad it's all coming together. – PeterKA Jul 27 '14 at 20:26
0

Insert this code in the function e():

console.log(t === jQuery);

You will assert that t is jQuery alias indeed in that function.

Niloct
  • 9,491
  • 3
  • 44
  • 57
0

As already mentioned above, t equals to the jquery object in this case. t(window) means, wrap the window object with the jquery object, so after wrapping it, i can call jquery methods on it. I'm sure you are already familiar with jquery's $(selector) method, which does the same (fair enough).

So, $('#product') should get the DOM element with id "product" and wrap it inside a jquery object.

Example:

var myProduct = document.querySelector('#product');
myProduct.attr('id'); // error, myProduct doesn't have method attr() because it is not a jquery "instance"
$(myProduct).attr(id); // product, we wrapped the item in a jquery object

Since jquery is named as t in your example, t(window) is wrapping the window object in jquery. The window object normally doesn't have a scrollTop() method defined on it.

Attila Kling
  • 1,717
  • 4
  • 18
  • 32
  • Yes and now I am not sure why the site programmer didn't just use "$" instead of the "t". That's where I got confused. I am too used to thinking this: jQuery(document).ready(function($){...some codes...} ( jQuery, window ); as the standard. – saldtch Jul 27 '14 at 20:35
  • Maybe it was just the result of some minification. By the way, you should stick to that form: (function($) { $(document).ready(function() { // code here }); }(jQuery)); – Attila Kling Jul 27 '14 at 20:49
  • Thanks! Somehow the reference I am learning from always use the form jQuery(document).ready(function($){...some codes...} ( jQuery, window ) – saldtch Jul 28 '14 at 13:30