-1

if you declare a function in javascript, you can't use the '$' character in the name, so how is that jquery is able to do so? For example:

function myFunction() {
    $("#h01").html("Hello jQuery")
}
$(document).ready(myFunction);

but if i declare a function as so:

function $(a){
// do something
}

javascript shows an error?

dave
  • 14,991
  • 26
  • 76
  • 110

5 Answers5

5

From the specification, https://es5.github.io/#x7.6

IdentifierStart ::
    UnicodeLetter
    $
    _
    \ UnicodeEscapeSequence

$ is fine as a character in the name.

Demo, using the code in the question: http://jsfiddle.net/q02go7dd/

guest
  • 6,450
  • 30
  • 44
3

try

var $ = function(){
    // do something
};

and you won't get an error.

prograhammer
  • 20,132
  • 13
  • 91
  • 118
0

$ is a short-handed alias for JQuery object itself.

So when you type,

$("#h01").html("Hello jQuery");

you are calling JQuery constructor with parameter "h01".

It's like declaring a function called dave and giving it an alias ß and calling it like that.

dave("h01");
Ömer Cinbat
  • 400
  • 2
  • 8
0

$ is fine :

$myFunction = function(){ alert("hello"); }

$myFunction(); // alerts "hello"
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • but by itself is not...so how is jquery able to use $ by itself? edit: nvm, i just tried var $=function(){} and it worked. – dave Feb 16 '15 at 20:34
-2

Jquery is a wrapper which runs over javascript. In spite of using javascript large syntax, Jquery offer small syntaxs which is easy to use.

In Jquery $ sign return Jquery object.

rahlrokks
  • 451
  • 1
  • 4
  • 12