Functions
-
The defining characteristic of a function—what distinguishes it from any other object—is the presence of an internal property named [[Call]].
- Internal properties are not accessible via code but rather define the behavior of code as it executes.
- ECMAScript defines multiple
internal properties for objects in JavaScript, and these internal properties
are indicated by double-square-bracket notation.
- The [[Call]] property is unique to functions and indicates that the
object can be executed
Declarations vs Expressions
There are actually two literal forms of functions.
-
The first is a function
declaration,
- which begins with the function keyword and includes the name
of the function immediately following it.
- The contents of the function are
enclosed in braces, as shown in this declaration
function add(num1, num2) {
return num1 + num2;
}
-
The second form is a function expression, which doesn’t require a name
after function.
- These functions are considered anonymous because the
function object itself has no name.
- Instead, function expressions are typically referenced via a variable or property, as in this expression:
var add = function(num1, num2) {
return num1 + num2;
};
-
Although these two forms are quite similar, they differ in a very important way.
- Function declarations are hoisted to the top of the context
- (either
the function in which the declaration occurs or the global scope) when the
code is executed.
- That means you can actually define a function after it is
used in code without generating an error.
For example:
var result = add(5, 5);
function add(num1, num2) {
return num1 + num2;
}
Examples
--------------------------------------------------------------------------------
// they are hoisted to the top of the context where it was declared or to the global context
console.log(mult(1,3)); // 3, -- Funcion Multiplicar --
function mult(n1,n2) {
console.log("-- Funcion Multiplicar --");
return n1 * n2;
}
--------------------------------------------------------------------------------
En esta caso console.log devuelve algo debido al hosting de la funcion
Function hoisting happens only for function declarations because the function name is known ahead of time
console.log(division(9,3)); // 3, -- Funcion Division --
function division(n1,n2) {
console.log("-- Funcion Division --");
return n1 / n2;
}
En este otro caso no es asi, por lo que la funcion "resta" no es una DECLARACION
si no mas bien una ASIGNACION y abra un error en consola
var resultado = console.log(resta(10,5));
var resta = function (n1,n2) {
console.log("-- Funcion Resta --");
return n1 - n2;
}
--------------------------------------------------------------------------------
function add(n1, n2) {
console.log("-- Funcion Suma 1 --");
return n1 + n2;
}
// ERROR!!
// el interprete me marca error por tener el
// mismo identificador varias veces declarado
var add = function (n1, n2) {
console.log("-- Funcion Suma 2 --");
return n1 + n2;
}
console.log(add(1, 3));
console.log(add(3, 3));
var add1 = add;
console.log(add1(6, 3));
https://playcode.io/664865
--------------------------------------------------------------------------------
-
Function hoisting happens only for function declarations because
the function name is known ahead of time.
- Function expressions, on the
other hand, cannot be hoisted because the functions can be referenced
only through a variable.
So this code causes an error:
// error!
var result = add(5, 5);
var add = function(num1, num2) {
return num1 + num2;
};
References
THE PRINCIPLES OF OBJECT-ORIENTED JAVASCRIPT. Copyright © 2014 by Nicholas C. Zakas