function popup1(name) {
alert('Halo' + ' ' + name);
}
popup1('John');
let popup2 = function(name) {
alert('Halo' + ' ' + name);
}
popup2('John');
const example1 = (name, time) => {
return `Good ${time}, ${name}`;
}
console.log(example1("John", "Night"));
Function with only one parameter and one line of return can be simplified. No need parantheses () for parameter, and no "return" to be stated.
Untuk function dengan satu parameter dan satu baris return, bisa dibuat simple, parameter tidak perlu (), "return" juga tidak perlu ditulis:
const example2 = name => `Halo ${name}`;
console.log(example2("John"));
Function without parameter shall use ()
Untuk function tanpa parameter harus digunakan ()
const halo = () => `Hello World!`;
console.log(halo());
Application of "this" with functions:
In this case the function will return undefined value when created. Case #1 not the correct implementation.
* Case #2: OuterFunction = function expression, InnerFunction = Arrow Function (This is the correct one / Ini yang benar)
In this case the function will return correct value when called. Case #2 is a good implementation.
In this case the function will return nothing when called. "this" is not applicable in arrow function. Case #3 is not correct way in creating object.
In this case the function will return nothing when called. "this" is not applicable in arrow function. Case #4 is not correct way in creating object.
a. Object Function Expression + this + Method Array Function -> Result: OK (similar to item h)
b. Object Literal + Method Function Expression -> Result: OK
c. Object Literal + Method Arrow Function -> Result: Undefined
d. Object Literal + Method Function Declaration inside function -> Result: Undefined
e. Object Literal + Method Arrow Function inside function -> Result: OK
f. Object Literal + Method Function Declaration inside function with .bind(this) -> Result: OK
g. Outer Function: Function Expression + this + Inner Function: Function Expression -> Result: Undefined
h. Outer Function: Function Expression + this + Inner Function: Arrow Function -> Result: OK (similar to item a)
i. Outer Function: Arrow Function + this + Inner Function: Arrow Function -> Result: Nothing (this is not recognized in arrow function (outer))
j. Outer Function: Arrow Expression + this + Inner Function: Function Expression -> Result: Nothing (this is not recognized in arrow function (outer))
k. Arrow Function in map method -> Result: OK