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 $?

3
  • stackoverflow.com/questions/1049112/… Commented Oct 2, 2016 at 9:57
  • @Jean-ClaudeColette not quite the same Commented Oct 2, 2016 at 9:57
  • 2
    If you think of $ as a synonym for jQuery then it becomes clear why "jQueryutil" doesn't work. Commented Oct 2, 2016 at 9:58

6 Answers 6

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"

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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.

Comments

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.

Comments

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).

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.