Skip to content

Commit 0bd0950

Browse files
committed
update
1 parent 38547c7 commit 0bd0950

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Functions/01_IFFE.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Global variable
2+
const a = 20;
3+
const b = 20;
4+
5+
// normally function
6+
function add() {
7+
console.log(`The result is : ${a + b}`); //40
8+
}
9+
add();
10+
11+
// Disini variable dia terproteksi
12+
// like local
13+
(function () {
14+
const a = 40;
15+
const b = 40;
16+
console.log(`The result is : ${a + b}`); //80
17+
})();
18+
19+
// use parameter
20+
(function (num1, num2) {
21+
console.log(`The result is : ${num1 * num2}`); //80
22+
})(500, 5000000); // 2500000000
23+
24+
// expression function
25+
const result = (function (num1, num2) {
26+
console.log(`The result is : ${num1 * num2}`); //80
27+
return num1 * num2;
28+
})(500, 5000000);
29+
30+
console.log(result); //2500000000 cause your saving return if not will undefined

Functions/02_hoisting.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Hoisting
2+
// function and var declaration are hoisted
3+
// safer to acces only after initialized
4+
5+
// this invoke mau disimpan dimana pun tetap terpanggil
6+
// karena js membaca stiap baris code
7+
8+
person();
9+
// console.log(person);
10+
11+
// var dia akan undefined
12+
// tapi tdk menghsilkan error
13+
// but kalo disimpan stlah functionnya dia
14+
// akan menghasilkan random
15+
personVar();
16+
17+
// jika simpan diatas sebelum function akan error
18+
// karena function dgn inisialiasi hnya bisa membaca
19+
// ketika hanya sesudah di inisialiasi
20+
// artinya lebih aman digunakan hanya sesudah di inisialiasi
21+
theName(); // error
22+
23+
let firstName = 'zoro';
24+
const friendName = 'lutfy';
25+
var random = 'random';
26+
27+
// theName(); //bisa krna sesudah inisialisasi
28+
29+
function person() {
30+
console.log(`Hello World`);
31+
}
32+
33+
function personVar() {
34+
console.log(`${random}`);
35+
}
36+
37+
function theName() {
38+
console.log(`${firstName} & ${friendName}`);
39+
}
40+
41+
// theName(); //bisa krna sesudah inisialisasi

Functions/03_closures.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// clousures
2+
// gives you an access to an outer functions scope
3+
// from an innner function
4+
// make private variables with closures
5+
6+
function shpName(firstName, lastName) {
7+
let member = 'SHP';
8+
function addName(balance) {
9+
console.log(
10+
`Hello member ${member} : ${firstName} ${lastName} with ${
11+
balance * 10000000
12+
}`
13+
);
14+
}
15+
return addName;
16+
}
17+
18+
// invoke, and use this () for calling inner function
19+
shpName('mugiwara', 'no lutfy')(100000000);
20+
21+
// create 2 instance obj
22+
const zoro = new shpName('rorona', 'zoro');
23+
const sanji = new shpName('sanji', 'vinsmoke');
24+
zoro(4000000);
25+
sanji(500000);

0 commit comments

Comments
 (0)