File tree 4 files changed +81
-16
lines changed
4 files changed +81
-16
lines changed Original file line number Diff line number Diff line change 3
3
// from an innner function
4
4
// make private variables with closures
5
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
- ) ;
6
+ function outer ( ) {
7
+ // membuat variable just for function
8
+ let privateVar = 'secret' ;
9
+ function inner ( ) {
10
+ console . log ( `hello world is ${ privateVar } ` ) ;
14
11
}
15
- return addName ;
12
+ // add return
13
+ return inner ;
14
+ inner ( ) ; //invoke
16
15
}
17
16
18
- // invoke, and use this () for calling inner function
19
- shpName ( 'mugiwara' , 'no lutfy' ) ( 100000000 ) ;
17
+ // console.log(privateVar); //error cz not defined
20
18
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 ) ;
19
+ // menggunakan () tuk memanggil juga innner
20
+ // jika tanpa () tidak akan menghasilkan apapun
21
+ outer ( ) ( ) ; // hello world
22
+
23
+ // atau mengguakan variable tuk memanggil innner
24
+ // const value = outer();
25
+ // value(); // will error solution use return inner function
Original file line number Diff line number Diff line change
1
+ // Closure basic example
2
+
3
+ function newAccount ( name , initialBalance ) {
4
+ let balance = initialBalance ;
5
+ function showDisplay ( ) {
6
+ console . log ( `Hello ${ name } with balance is ${ balance } ` ) ;
7
+ }
8
+ // you should
9
+ return showDisplay ;
10
+ }
11
+
12
+ // invoke it, and () => for invoke it showDisplay
13
+ newAccount ( 'jane' , 550000 ) ( ) ;
14
+
15
+ // or you can create new 2 instance obj
16
+ const kelly = new newAccount ( 'kelly' , 450000 ) ;
17
+ const melly = new newAccount ( 'melly' , 345000 ) ;
18
+
19
+ // invoke it
20
+ kelly ( ) ;
21
+ melly ( ) ;
Original file line number Diff line number Diff line change
1
+ // closure complete
2
+
3
+ function newAccount ( name , initialBalance ) {
4
+ let balance = initialBalance ;
5
+ function showBalance ( ) {
6
+ console . log ( `Hello ${ name } your balance is ${ balance } ` ) ;
7
+ }
8
+ function deposite ( amount ) {
9
+ balance += amount ;
10
+ showBalance ( ) ;
11
+ }
12
+ function withDraw ( amount ) {
13
+ // *** IF amount bigger than balance
14
+ if ( amount > balance ) {
15
+ console . log (
16
+ `Sorry ${ name } your amount : ${ amount } bigger than ${ balance } `
17
+ ) ;
18
+ return ;
19
+ }
20
+ // false
21
+ balance -= amount ;
22
+ showBalance ( ) ;
23
+ }
24
+ return { showBalance : showBalance , deposite : deposite , withDraw : withDraw } ;
25
+ }
26
+
27
+ // invoke it
28
+ const john = new newAccount ( 'john' , 3000 ) ;
29
+
30
+ john . showBalance ( ) ; // 3000
31
+ john . deposite ( 3000 ) ; // 6000
32
+ john . withDraw ( 7000 ) ; // Sorry john your amount : 7000 bigger than 6000
33
+ // cause amount (6000) bigger than from 6000 (deposite)
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6
+ < title > Document</ title >
7
+ </ head >
8
+ < body >
9
+ < script src ="./03_closures.js "> </ script >
10
+ </ body >
11
+ </ html >
You can’t perform that action at this time.
0 commit comments