-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
  • 13,917
  • 25
  • 68
  • 107

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,135
  • 27
  • 41
3

try

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

and you won't get an error.

prograhammer
  • 18,956
  • 13
  • 86
  • 115
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
  • 389
  • 2
  • 8
0

$ is fine :

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

$myFunction(); // alerts "hello"
Jeremy Thille
  • 25,196
  • 9
  • 41
  • 59
  • 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
  • 371
  • 1
  • 2
  • 12