0

I'm writing a utility file and I've gotten some examples from online and this a form of writing the utility I've come across:

$.util = $.extend($.util || {}, {

    //functions here...

});

and so I think I understand what it's doing. It allows me to call $.util.function() somewhere else, however when I remove the . in front of the $ the code breaks. What does this notation mean? What's the difference between $. and $?

Van-Sama
  • 1,154
  • 4
  • 14
  • 21

6 Answers6

6

$.util = something means "assign something to property util of object $".

$util = something means "assign something to variable $util"

Similarly, $.extend is "get value of property extend of object $" (which is a function in this exact scenario) and $extend is "get value of variable $extend"

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • ohh shoot. I thought that adding `$` onto any variable would automatically make it jQuery. I'm not sure why I thought that but that makes sense. Thank you! – Van-Sama Oct 02 '16 at 09:58
4

If you're using jQuery, $ is just a variable contaning the jQuery object. So by writing $., you're essentially accessing jQuery properties and functions. Instead of $, you could also write jQuery and it should work the same way.

There's no special meaning to the $ character in JavaScript other than that. It acts like any other character, so $util is just a variable name.

Andii
  • 337
  • 1
  • 5
  • 19
3

jQuery is an object that is assigned to both jQuery and $ on the window

It has methods that act on collections of elements eg $('.some-element').someMethod() and static methods that are just attached to the jQuery object but don't modify a collection, They are just normal function attached to the jQuery object to prevent exposing too many functions to the global context.

synthet1c
  • 6,152
  • 2
  • 24
  • 39
2
  • $. - allows you to proceed to $ (jQuery) object property or method directly
  • $ - usually used as shortcut for invoking jQuery object

Whilst prefixing anything with $ won't make it jQueryable bec. this character can be used in variable name along with others (e.g. what is not applicable for PHP).

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
2

Consider jQuery as a big class woth a lot of static functions and constructs.

The right way for calling any of its functions should be jQuery.someFunc() for static functions and var obj = jQuery('css selectors') for creating an object for HTML objects and then executing functions on that object.

Now for easier coding, jQuery added $ as an alias for jQuery. It's nothing more than an alias.

AwesomeGuy
  • 1,059
  • 9
  • 28
2

Try this code:

  <script type="text/javascript">
    document.write('(jQuery === $) is ' + (jQuery === $) + '<br />');
    document.write('typeof(jQuery) = ' + typeof(jQuery) + '<br />');
  </script>

You will see:

(jQuery === $) is true

typeof(jQuery) = function

So jQuery is a function with a bunch of extra properties and functions attached to it.

If you're coming from a strongly-typed language background, the concept of attaching properties and methods to a function might seem strange, but you can do it in javascript.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205