Skip to content

Commit b334bcb

Browse files
committed
update
1 parent 5a5930e commit b334bcb

14 files changed

+299
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Arrow functions or Fat arrow functions
2+
// reg function : 'this' refers parent, left of the dot
3+
// arrow function : referes to it's cureent surrounding scope
4+
5+
// first example use re function
6+
7+
const btn = document.querySelector('.btn');
8+
// console.log(btn);
9+
10+
// Disini dia bisa merubah warna menggunakan reg function
11+
// karena this parentnya merujuk pada btn dot
12+
// btn.addEventListener('click', function () {
13+
// this.style.background = 'black';
14+
// });
15+
16+
// sedangkan menggunakan arrow dia akan error
17+
// karena sebuah this nya dia merujuk pada window obj()
18+
// btn.addEventListener('click', () => {
19+
// // console.log(this); global window obj {}
20+
// this.style.background = 'black';
21+
// });
22+
23+
// terkecuali kita membuat setTimeout 2seconds
24+
// bisa menggunakan arrow karna dalam setTimeout
25+
// arrow function merujuk pada sekitaran scope
26+
// sedangkan jika mnggunakan function dia bukn mrujuk
27+
// pada parrent tapi window global obj()
28+
btn.addEventListener('click', function () {
29+
setTimeout(() => {
30+
this.style.background = 'navy';
31+
}, 2000);
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// default paramaters, arrow function gothcas
2+
3+
const john = 'john';
4+
sayHi(); // not error cz john already declare
5+
const peter = 'peter';
6+
7+
function sayHi(person = john) {
8+
console.log(`Hi ${person}`);
9+
}
10+
11+
sayHello(); //error cz sayHello not declare
12+
const sayHello = (person = 'bobo') => console.log(`Hello ${person}`);

ES6/10_array_destructing.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Destructuring
2+
// faster/easier way to access/unpack values from arrays
3+
// objects into variables
4+
// Arrays
5+
6+
const fruits = ['orange', 'banana', 'lemon'];
7+
const friends = ['john', 'peter', 'bob', 'anna', 'kelly'];
8+
9+
const orange = fruits[0];
10+
const banana = fruits[1];
11+
const lemon = fruits[2];
12+
13+
// console.log(orange, banana, lemon);
14+
15+
// example array assign to friends
16+
// susan disini menjadi undefined karena index friend hanya
17+
// berisi 4 index 0-4 sehingga susan menjadi undefined
18+
// const [john, peter, bob, anna, kelly, susan] = friends;
19+
// console.log(john, peter, bob, anna, kelly, susan);
20+
21+
// dan untuk menskip misal peter anna di hilangkan
22+
// dalam pendeklarasian const hilangkan dgn comma
23+
const [john, , bob, , kelly] = friends;
24+
console.log(john, bob, kelly); //john, bob, kelly
25+
26+
/*
27+
note const [john, bob, ...] nama bebas karna mau nama apapun
28+
dia akan menghasilkan value yang berada dalam array friends
29+
30+
*/

ES6/11_swap_variables.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Destructuring swap variables
2+
// faster/easier way to access/ unpack from arrays
3+
// objects into variables
4+
// arrays
5+
6+
let first = 'bob';
7+
let second = 'john';
8+
9+
// let temp = second;
10+
// second = first; //bob
11+
// first = temp; // john
12+
13+
// artinya adalah
14+
// second untuk first / john
15+
// first untuk second / bob
16+
[second, first] = [first, second];
17+
18+
console.log(first, second); // john bob

ES6/12_obj_destructuring.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Destructuring
2+
// faster/easier way to access/ unpack from arrays
3+
// objects into variables
4+
// objects
5+
6+
const bob = {
7+
first: 'bob',
8+
last: 'sanders',
9+
city: 'chicago',
10+
siblings: {
11+
sister: 'jane',
12+
},
13+
};
14+
15+
// sama dgn swap variable array tadi
16+
// dan disini juga bisa menggunakan first:firstName
17+
// firstName adalah semacam tag
18+
// dan tuk mengakses jane menggunakan function
19+
// siblings:{sister:nametag}
20+
const {
21+
first: firstName,
22+
last,
23+
city,
24+
siblings: { sister: favoriteSibling },
25+
} = bob;
26+
console.log(firstName, last, city, favoriteSibling);
27+
28+
// const firstName = bob.first;
29+
// const lastName = bob.last;
30+
// const sister = bob.siblings.sister;
31+
// console.log(firstName, lastName, sister);
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Destructuring
2+
// faster/easier way to access/ unpack from arrays
3+
// objects into variables
4+
// objects
5+
6+
const bob = {
7+
first: 'bob',
8+
last: 'sanders',
9+
city: 'chicago',
10+
siblings: {
11+
sister: 'jane',
12+
},
13+
};
14+
15+
function printPerson({ first, last, city }) {
16+
console.log(first, last, city);
17+
}
18+
19+
printPerson(bob); // bob sanders chicago

ES6/14_new_string_method.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// new string methods
2+
// startsWith(), endsWith(), includes(), repeat()
3+
4+
const person = 'Peter Smith';
5+
const employee = '23456-EMP-PETER-SMITH';
6+
const manager = '23456-MAN-JOHN-DOE';
7+
8+
// remember this case sensitive
9+
10+
// startsWith()
11+
console.log(person.startsWith('Pe')); //true
12+
console.log(person.startsWith('pe')); //false
13+
// 6 and hitung dimulai dari 23456- sebelum EMP total 6
14+
console.log(employee.startsWith('EMP', 6)); //true
15+
16+
//endsWith()
17+
console.log(manager.endsWith('DOE')); //true
18+
console.log(manager.endsWith('doe')); //false
19+
// hitung dari awal sampai sesudah john
20+
console.log(manager.endsWith('JOHN', 14)); //true
21+
22+
// repeat()
23+
const theRepeat = (person, amount = 3) => person.repeat(amount);
24+
25+
const multiple = theRepeat(person, 20);
26+
console.log(multiple);

ES6/15_for_of_loop.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// for of - loops through the values of an iterable objc
2+
// string, arrayy, map, set , etc - not object
3+
// unlike foreach - we can use break , continue
4+
5+
const fruits = ['apple', 'orange', 'banana', 'peach'];
6+
const longName = 'john smith pepper III';
7+
let shortName = '';
8+
9+
for (const letterName of longName) {
10+
if (letterName === ' ') {
11+
continue;
12+
} else {
13+
shortName += letterName;
14+
}
15+
}
16+
// console.log(shortName); //johnsmithpepperIII
17+
18+
for (const fruit of fruits) {
19+
if (fruit === 'banana') {
20+
// continue;
21+
break;
22+
}
23+
console.log(fruit); // apple, orange, peach
24+
// jika continue banana dilewat
25+
// jika break stop sampai orange
26+
}

ES6/16_spread_operator.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Spread operator
2+
// Allows an iterable to spread / expand individually inside reciever
3+
// Split into single items and copy them
4+
5+
const udemy = 'udemy';
6+
7+
const letter = [...udemy]; // [ 'u', 'd', 'e', 'm', 'y' ]
8+
9+
// console.log(letter);
10+
11+
const boys = ['john', 'peter', 'bob'];
12+
const girls = ['susan', 'anna'];
13+
const bestFriend = 'arnold';
14+
15+
const friend = [...boys, ...girls, bestFriend];
16+
console.log(friend);
17+
// [ 'john', 'peter', 'bob', 'susan', 'anna', 'arnold' ]
18+
19+
// reference
20+
// const newFriends = friend;
21+
const newFriends = [...friend]; //but if use spread friend still use originall array
22+
newFriends[0] = 'jane';
23+
console.log(newFriends); //john will change be jane
24+
console.log(friend); //john will change be jane cz with allocate same memory if use re-assign

ES6/17_spreadoperator_objects.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// spread operator....
2+
// Allows an iterable to spread/expand indvidually inside reciever
3+
// split into single items and copy them.
4+
// ES2018/ ES8
5+
6+
const person = { name: 'mugiwara', job: 'pirates' };
7+
let copyPerson = { ...person };
8+
console.log(copyPerson);
9+
// dan tuk menambah obj bisa langsung
10+
copyPerson = { ...person, age: '25', name: 'rorona zoro' };
11+
console.log(copyPerson); // {name:'rorona zoro',job:'pirates',age:25}
12+
console.log(person); //still orignal {name:'mugiwara',job:'pirates'}

ES6/18_spread_obj.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const title = document.querySelectorAll('h1');
2+
const result = document.querySelector('.result');
3+
4+
// solusi harus dibuatkan array krna masih node-list
5+
// kemudian baru bisa menggunakan map,
6+
// gunakan template string dan jgnlupkan .join
7+
const titles = [...title]
8+
.map((item) => `<span>${item.textContent}</span>`)
9+
.join('');
10+
// console.log(titles); // array [h1,h1,h1]
11+
12+
result.innerHTML = titles;
13+
// JavaScripthtml5css3

ES6/19_spread_function-argument.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const numbers = [90, 34, 78, 54, 556];
2+
3+
// will error because you should spread operator
4+
// console.log(Math.max(numbers)); // NaN
5+
// console.log(Math.max(...numbers)); //556
6+
7+
// example again use function
8+
// actually concept still same
9+
10+
const fullName = ['mugiwara no', 'lutfy'];
11+
const person = (firstName, lastName) => {
12+
console.log(`Hello ${firstName} ${lastName}`);
13+
};
14+
15+
// person(fullName[0], fullName[1]);
16+
person(...fullName); //effective use spread

ES6/20_rest_operator.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Rest operator
2+
// collcets the items
3+
// note jangan menyimpan rest di depan
4+
// selalu simpan rest operator di belakang selalu
5+
6+
// arrays
7+
const shp = ['mugiwara', 'zoro', 'sanji'];
8+
const [pirates, second, ...rest] = shp;
9+
// mugiwara zoro [ 'sanji' ]
10+
// console.log(pirates, second, rest);
11+
12+
// obj
13+
const person = {
14+
name: 'shanks',
15+
member: 'RHP',
16+
};
17+
18+
const { name: firstName, member, ...rest_ } = person;
19+
// console.log(firstName, member, rest_); //shanks RHP {}
20+
21+
// function
22+
const allbanking = [10, 10, 10];
23+
function getPerson(fullName, ...banking) {
24+
console.log(fullName, banking);
25+
let total = 0;
26+
for (const value of banking) {
27+
total += value;
28+
}
29+
console.log(`Hello ${fullName} your banking is ${total}`);
30+
}
31+
32+
getPerson(person.name, 10, 10, 10);
33+
getPerson(person.name, ...allbanking);

ES6/index.html

+7-21
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,14 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Document</title>
7-
<style>
8-
body {
9-
text-transform: capitalize;
10-
display: grid;
11-
place-items: center;
12-
min-height: 100vh;
13-
}
14-
15-
.btn {
16-
background: red;
17-
color: #fff;
18-
border-radius: 0.5rem;
19-
text-transform: capitalize;
20-
cursor: pointer;
21-
font-size: 1.5rem;
22-
padding: 1rem 4rem;
23-
}
24-
</style>
7+
<style></style>
258
</head>
269
<body>
27-
<button class="btn">click me</button>
10+
<h1>JavaScript</h1>
11+
<h1>html5</h1>
12+
<h1>css3</h1>
13+
<div class="result"></div>
2814
</body>
29-
<!-- <script src="./06_arrow_functions_basics.js"></script> -->
30-
<script src="./07_arrow_functions_object_this.js"></script>
15+
<!-- <script src="18_spread_obj.js"></script> -->
16+
<script src="20_rest_operator.js"></script>
3117
</html>

0 commit comments

Comments
 (0)