When it comes to JavaScript, an object serves as a key-value mapping where keys are strings and values can range from anything, making them ideal for hashmaps.
Functions, on the other hand, function as regular objects but with the added capability of being called upon.
SOURCE https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#.22Normal.22_objects.2C_and_functions
This enables various functionalities such as:
function test(){
console.log(1);
}
var a = test;
a();
or
var test2 = function(){
console.log(2);
}
or autocall
// Apologies for the indentation.
(
function(){
console.log(3);
}
)()
We can also create data structures like:
var testHash = {
a : 1,
b : function(){
console.log(4);
}
}
testHash.b();
testHash['b']();
And even create functions that are harder 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
UPDATE: The user is interested in learning more about autocall functions:
Why does this work?
(
function(){
console.log(3);
}
)()
It's easy to understand in two steps:
The parentheses, which are used for grouping or calling functions when we think of functions as variables.
var test_1 = 'string example';
var length = (test_1).length; // same as test_1.length
For instance:
var test_1 = 'string';
var test_2 = ' example';
var length = (test_1 + test_2).length; // same as test_1.length
as opposed to:
var test_1 = 'string';
var test_2 = ' example';
var aux = test_1 + test_2;
var length = aux.length; // same as test_1.length
Now, does this make sense to you?:
var length = ('string example').length; // instead of the first example
Second step, change the string to a function.. and then call it
( function(){ ... } )()
Why is this interesting?
Well, now the concept of closure comes into play.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
Closures prove to be a pivotal tool in JavaScript programming.