Mike
Mike

Reputation: 2614

How does jQuery.get() work?

I'm trying to use this: http://api.jquery.com/jQuery.get/ and I don't understand why the examples look like this:

 $.get("test.php");

I've never seen the syntax $.get ? Why wouldn't I do something like

$jQuery = new JQuery();
$jQuery.get(...); 

Upvotes: 1

Views: 1106

Answers (4)

SLaks
SLaks

Reputation: 887275

The $ variable is created by jQuery and is the same as the jQuery function / object.

You're thinking of jQuery's instance methods, which operate on jQuery objects that contain DOM elements.

$.get is essentially a static method, since it doesn't operate on a set of DOM elements.
There would be no point in calling it on a jQuery set instance, so it is called without one instead.

Upvotes: 2

Alnitak
Alnitak

Reputation: 339786

It's simple - jQuery automatically creates the variable $ pointing at at jQuery when it's first loaded.

Hence:

$.get(...);

is example the same as:

jQuery.get(...);

If you don't want $ to point at jQuery (perhaps because it's also being used by another library), look at $.noConflict()

Upvotes: 0

js-coder
js-coder

Reputation: 8336

$ or jQuery is an object, that allows you to use some methods like get.

The new keyword is usually used with a function that acts like a class. The new keyword sets this to the variable name you assign it.

There is no reason why you would need another jQuery object (except when you want to use another libary, but there is jQuery.noConflict). All you need is in the jQuery object.

Upvotes: 0

Eric
Eric

Reputation: 97565

The $ symbol (equivalent to jQuery) is not a constructor, it's a function and an object. As such, there is no need to use new on it.


$('css selector').get() returns an array of dom elements that the selector matched.

$.get() does an HTTP GET request

Upvotes: 4

Related Questions