Skip to content

Latest commit

 

History

History
127 lines (63 loc) · 5.1 KB

File metadata and controls

127 lines (63 loc) · 5.1 KB

3-Ways of Writing Function in Javascript

1. Function Declaration

    function popup1(name) {
        alert('Halo' + ' ' + name);
    }
    popup1('John');

2. Function Expression

    let popup2 = function(name) {
        alert('Halo' + ' ' + name);
    }
    popup2('John');

3. Arrow Function

a. Multilines Arrow Function

    const example1 = (name, time) => {
        return `Good ${time}, ${name}`;
    } 
    console.log(example1("John", "Night"));

b. Single Line Arrow Function

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());

c. "this" in Arrow Function

Application of "this" with functions:

* Case #1: OuterFunction = function expression, InnerFunction = function expression

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.

* Case #3: OuterFunction = Arrow Function, InnerFunction = Arrow Function

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.

* Case #4: OuterFunction = Arrow Function, InnerFunction = Function Expression

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.

Summary of Guideline in Creating Functions

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