Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript, functions vs variables

Tags:

javascript

I'm starting to look into Javascript and JQuery (hence my choice of example below). And I found that I could define a function and call it (as expected), but that I could also just .. Do something else.. And that's the question:

function $() {
    console.log('hi');
}

$()
$

I don't get an error with either the function call or by just stating '$' without calling the function. What is the latter actually doing? And why does it work if it isn't actually calling the function?

like image 334
Zach Smith Avatar asked May 28 '15 22:05

Zach Smith


People also ask

What is the difference between functions and variables?

A variable stores a value, and a function is a program (can't think of another word for it). So you can have a variable of n which stores the value 1, and you can have a function called print(n) that will prints whatever is inside the parenthesis, (n in this example) so the value stored of n is printed.

What are the 3 types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.

Can we use a function as a variable value in JavaScript?

Programmers can create a single variable using the var, let, or const keywords and assign the function expression to that. However, creating a variable with the const keyword is recommended to assign the function as the function expression always remains constant.

Can a function be stored as a variable in JavaScript?

In JavaScript, functions are called Function Objects because they are objects. Just like objects, functions have properties and methods, they can be stored in a variable or an array, and be passed as arguments to other functions.


3 Answers

It does nothing. It's just a variable that happens to hold a function.

It's no different from the following equally useless code:

42;
like image 145
SLaks Avatar answered Oct 04 '22 23:10

SLaks


A JavaScript object is a mapping between keys and values. Keys are strings and values can be anything. This makes objects a natural fit for hashmaps.

Functions are regular objects with the additional capability of being callable.

FROM https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#.22Normal.22_objects.2C_and_functions

This mean that you can do things like:

function test(){
   console.log(1);
}

var a = test;

a();

or

var test2 = function(){
  console.log(2);
}

or autocall

//sorry for the indentation.
(
  function(){
     console.log(3);
  }
)()

Or create structures

var testHash = {
   a : 1,
   b : function(){
      console.log(4);
   }
}

testHash.b();

testHash['b']();

And create function difficult to call:

//in a browser environment
window['test3'] = function(){
   console.log(5);
} 

window['test space'] = function(){
   console.log(6);
} 

test3() //no error
test space() //error :D

EDIT: The user wants to know more about autocall functions:

Why this work?

(
  function(){
     console.log(3);
  }
)()

It easy to follow in 2 steps:

The parenthesis, if we know that a function is like other variables, and we know that the parenthesis is only for made groups or call functions.

var test_1 = 'string example';
var length = (test_1).length; // the same that test_1.length

Make sense in:

var test_1 = 'string';
var test_2 = ' example';
var length = (test_1 + test_2).length; // the same that test_1.length

instead of:

var test_1 = 'string';
var test_2 = ' example';
var aux = test_1 + test_2;
var length = aux.length; // the same that test_1.length

Now, Do this make sense for you?:

var length = ('string example').length; // instead the first example

Second step, we can change the string for the function.. and call it

( function(){ ... } )()

why is this interesting? Well, now appear the concept of closure.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

The closures are a very important tool in javascript.

like image 23
Raúl Martín Avatar answered Oct 05 '22 00:10

Raúl Martín


the name "$" is just the holder of the function, by doing the line "$" it is just to list the content of the code (in Google Chrome's developer tool).

$() is to call the function.

$ is to state what it is holding.

like image 26
phdfong - Kenneth Fong Avatar answered Oct 04 '22 23:10

phdfong - Kenneth Fong