Skip to content

Commit bcbf6c8

Browse files
committed
ES6 Complete
1 parent 92293d0 commit bcbf6c8

File tree

10 files changed

+409
-10
lines changed

10 files changed

+409
-10
lines changed

21. Github Finder/js/script.js

-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ async function getUserObject(file) {
5858
}
5959

6060
async function getUser(username) {
61-
62-
console.log(username);
6361
let usrDet = await getUserObject(`https://api.github.com/users/${username}`);
6462

6563
if (usrDet.message === "Not Found") {

22. ES6/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626

2727
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
28-
<script src="js/symbol_op.js"></script>
28+
<script src="js/map.js"></script>
29+
<!-- <script src="js/set.js"></script> -->
30+
<!-- <script src="js/promise.js"></script> -->
31+
<!-- <script src="js/symbol_op.js"></script> -->
2932
<!-- <script src="js/rest_operator.js"></script> -->
3033
<!-- <script src="js/spread_operator.js"></script> -->
3134
<!-- <script src="js/objectdestructing.js"></script> -->

22. ES6/js/map.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Map is data structure where value are stored in key/value pairs
2+
// Like Object
3+
// In set key and value are same but in map keys and values not need to be same
4+
5+
6+
// declare map
7+
8+
let map = new Map([
9+
["Name", "Abdullah"],
10+
["Age", 26]
11+
]);
12+
13+
// Add item in map with set Function(key, value)
14+
map.set("dob", "1998-10-23");
15+
16+
console.log(map);
17+
18+
//Get Item value from map
19+
console.log(map.get("Name"));
20+
21+
//Chek key exists in map
22+
console.log(map.has("Name"));
23+
console.log(map.has("Location"));
24+
25+
// Iterate Map
26+
27+
//print each item key value pair
28+
for( x of map){
29+
console.log(x);
30+
}
31+
32+
// print key and value
33+
for([x,y] of map){
34+
console.log(x,y);
35+
}
36+
37+
// print only key
38+
console.log("\nPrint only key");
39+
for(x of map.keys()){
40+
console.log(x);
41+
}
42+
43+
44+
console.log("\nPrint only values");
45+
for(x of map.values()){
46+
console.log(x);
47+
}
48+
49+
//print each item with entries
50+
console.log("\nPrint map item");
51+
for(x of map.entries()){
52+
console.log(x);
53+
}
54+
console.log("\nPrint key value with entries()");
55+
for([x,y] of map.entries()){
56+
console.log(x,y);
57+
}
58+
59+
// map to array
60+
console.log("\nMap to array key value(items)");
61+
let arr = Array.from(map);
62+
console.log(map);
63+
64+
console.log("\nMap to array key");
65+
arr = Array.from(map.keys());
66+
console.log(arr);
67+
68+
console.log("\nMap to array value");
69+
arr = Array.from(map.values());
70+
console.log(arr);
71+
72+
73+
console.log("\nIterate with foreach");
74+
// In foreach
75+
// 1st parameter is: value
76+
// 2nd parameter is: key
77+
// 3rd parameter is: whole map
78+
79+
map.forEach((value, key,map) => {
80+
console.log(key,value,map);
81+
});

22. ES6/js/promise.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Promises
2+
// Successful -> resolve
3+
// Failure -> reject
4+
5+
let prom = new Promise((resolve, reject) => {
6+
a = 1 + 1;
7+
if (a === 2) {
8+
resolve("Successful");
9+
}
10+
reject("Failed");
11+
});
12+
13+
14+
prom.then(result => {
15+
console.log(result);
16+
}).catch(err => {
17+
console.log(err);
18+
});
19+
20+
// It use when we need large number of file from server. When it executes other code execution work as well
21+
console.log("After Promises");
22+
23+
24+
// Promis run asyncronously
25+
26+
let prom2 = new Promise((resolve, reject) => {
27+
setTimeout(() => {
28+
a = 1 + 1;
29+
if (a === 2) {
30+
resolve("Successful 2nd");
31+
}
32+
reject("Failed");
33+
}, 2000)
34+
});
35+
36+
37+
prom2.then(result => {
38+
console.log(result);
39+
}).catch(err => {
40+
console.log(err);
41+
});
42+
43+
console.log("After 2nd Promises");
44+
45+
// Fetch
46+
// fetch return a promise
47+
48+
49+
// fetch('https://api.github.com/users/coderzaman')
50+
// .then(response => {
51+
// console.log(response.json());
52+
// })
53+
// .catch(err=>{
54+
// console.log(err);
55+
// })
56+
57+
58+
// feth API with async function
59+
60+
61+
62+
// fetchApi().then(response => {
63+
// console.log(response.bio);
64+
// });
65+
66+
async function fetchApi(file){
67+
let fetachObject = await fetch(file);
68+
69+
let fetchUser = await fetachObject.json();
70+
return fetchUser;
71+
}
72+
async function getUser(){
73+
let user = await fetchApi('https://api.github.com/users/coderzaman');
74+
console.log("here:", user);
75+
76+
if(user.message != "Not Found"){
77+
console.log(user.login);
78+
}
79+
80+
}
81+
getUser();

22. ES6/js/set.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Set: We can not assign duplicate values
2+
3+
let set = new Set([1, 2, 3,3]);
4+
set.add(4);
5+
set.add(5);
6+
console.log(set);
7+
8+
set.delete(1);
9+
console.log(set);
10+
11+
// has function ensure value exists in set or not
12+
13+
console.log(set.has(1));
14+
console.log(set.has(2));
15+
// set.clear(); // remove all set Items
16+
17+
// Iterate set
18+
19+
for( x of set){
20+
console.log(x);
21+
}
22+
23+
// Itrate set keys and values
24+
// set keys and values are same
25+
console.log("set keys");
26+
for( x of set.keys()){
27+
console.log(x);
28+
}
29+
30+
console.log("set values");
31+
for( x of set.values()){
32+
console.log(x);
33+
}
34+
35+
36+
console.log("set Iterator");
37+
let iter = set.entries();
38+
console.log(iter.next().value[0]);
39+
console.log(iter.next().value[0]);
40+
console.log(iter.next().value[0]);
41+
42+
43+
console.log("Loop with iterators");
44+
iter = set.entries();
45+
for ([x] of iter){
46+
console.log(x);
47+
}
48+
console.log("Loop with Foreach");
49+
set.forEach((keys, values) => console.log(values,keys));
50+

22. ES6/js/symbol_op.js

+99-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,105 @@ console.log(sym1 === sym2); // false
1919
// Iteration and Hidden Properties: Properties keyed by symbols are not enumerable in for...in loops or Object.keys(), so they can be used to create "hidden" properties.
2020

2121
let obj = {
22-
[Symbol('hidden')]: 'secret',
23-
name: 'public'
24-
};
25-
26-
for (let key in obj) {
27-
console.log(key); // logs 'name', but not the Symbol property
28-
}
22+
[Symbol('hidden')]: 'secret',
23+
name: 'public'
24+
};
25+
26+
for (let key in obj) {
27+
console.log(key); // logs 'name', but not the Symbol property
28+
}
2929
let symbols = Object.getOwnPropertySymbols(obj);
3030
console.log(symbols); // [Symbol(hidden)]
3131
console.log(obj[symbols[0]]); // 'secret'
32+
33+
let a = Symbol();
34+
35+
let person = {
36+
name: "Abdullah",
37+
age: 25,
38+
[a]: "Hdden"
39+
}
40+
41+
42+
console.log(person);
43+
44+
// Symbol are not generally member of the Object
45+
console.log(Object.getOwnPropertyNames(person, 'name'));
46+
47+
// Access symbols from the Object
48+
console.log(Object.getOwnPropertySymbols(person));
49+
50+
// If you chek either symbol is object property or not
51+
console.log(Object.keys(person));
52+
console.log(JSON.stringify(person));
53+
54+
for (p in person) {
55+
console.log(person[p]);
56+
}
57+
58+
59+
// Symbol Iterator
60+
let iterable = "Hello, world!";
61+
62+
// Symbol.iterator
63+
let iter = iterable[Symbol.iterator]();
64+
65+
let arr = [10, 20, 40, 50, 54];
66+
iter = arr[Symbol.iterator]();
67+
console.log(iter.next().value);
68+
console.log("Hello, world!");
69+
console.log(iter.next());
70+
console.log(iter.next());
71+
console.log(iter.next());
72+
console.log(iter.next());
73+
console.log(iter.next());
74+
75+
// Make a custome iterator
76+
77+
78+
function customeIterator(arr) {
79+
let index = 0;
80+
81+
return {
82+
next: function () {
83+
return index < arr.length ? { value: arr[index++], done: false } : { value: undefined, done: true };
84+
}
85+
}
86+
}
87+
88+
iter = customeIterator(arr);
89+
console.log(iter.next());
90+
console.log(iter.next());
91+
92+
// Generators: It is also types of iterators
93+
94+
function* generator(){
95+
yield 1;
96+
yield 2;
97+
yield "Abdullah",
98+
yield "Abdur Rahman"
99+
}
100+
101+
let gen = generator();
102+
console.log(gen.next().value);
103+
console.log(gen.next().value);
104+
console.log(gen.next().value);
105+
console.log(gen.next().value);
106+
107+
function* generator2(){
108+
yield 1;
109+
console.log("Hello world!");
110+
console.log("Hello world!");
111+
console.log("Hello world!");
112+
yield 2;
113+
yield "Abdullah",
114+
yield "Abdur Rahman"
115+
}
116+
117+
118+
let gen2 = generator2();
119+
120+
// it print also statements befor another yeiled
121+
console.log(gen2.next().value);
122+
console.log(gen2.next().value);
123+
console.log(gen2.next().value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
A framework provides rules and regulations (or guidelines) that developers follow when building an application. These rules help ensure consistency, maintainability, and best practices. It often dictates how the overall structure of the application should be organized, as well as how different components (like data, user interfaces, and logic) should interact with each other.
2+
3+
In short, the framework sets up a foundation or blueprint for the developer to work within, but it often requires following its conventions to achieve the desired outcome efficiently.
4+
5+
A library is a collection of pre-built functions or modules that you can use to solve specific problems or perform tasks. When you use a library, you are calling specific pieces of functionality as needed, rather than following a structured set of rules like a framework.
6+
7+
For example, in JavaScript:
8+
9+
The Math library provides functions like Math.sqrt() or Math.random() to perform mathematical operations.
10+
You use the functions or modules from the library when and where you need them, but you control the flow of the program.
11+
So, the key difference is that:
12+
13+
A framework often dictates the structure of your application (it calls your code).
14+
A library is something you call to use its functionality to solve a problem.
357 KB
Loading

0 commit comments

Comments
 (0)