Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should a period be used after $ in jQuery?

Tags:

I am a beginner in JQuery.

When should the dot after $ be used?

$.trim(str) and not $trim(str)?

And some cases without point: $(document).ready, but not $.(document).ready?

Thanks.

like image 778
John Avatar asked Aug 01 '10 22:08

John


People also ask

What does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.

What are periods used for in JavaScript?

JavaScript provides two notations for accessing object properties. The first, and most common, is known as dot notation. Under dot notation, a property is accessed by giving the host object's name, followed by a period (or dot), followed by the property name.

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

Does jQuery have action after time?

Answer: Use the jQuery delay() method You can use the jQuery delay() method to call a function after waiting for some time. Simply pass an integer value to this function to set the time interval for the delay in milliseconds.


1 Answers

In jQuery, $ is a variable name, like foo or bar, which references the global jQuery object. If you want to access a property on the jQuery object, then you use the dot. These are basically the same:

$.property jQuery.property 

(Since $ is a variable like any other, it is possible for you to set it to anything you like. This might happen if you were to include the Prototype library after including the jQuery library, which would leave you with $ pointing to an alias of the document.getElementById function.)

The jQuery object happens to also be a function, so you can call that function the same as you would call any other function:

$(arguments) alert("arguments!") 

On the other hand, $trim is just another global variable, like $ or location, which could be a function you defined yourself:

var $trim = function(arguments) {     return "foo"; };  $trim(str) 
like image 155
Douglas Avatar answered Oct 13 '22 21:10

Douglas