Skip to content

Commit 92293d0

Browse files
committed
spread, rest and symbol(basic)
1 parent c7a9751 commit 92293d0

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-2
lines changed

22. ES6/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121

2222
<body>
2323

24-
<div class="container not_found"></div>
24+
25+
26+
2527
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
26-
<script src="js/objectdestructing.js"></script>
28+
<script src="js/symbol_op.js"></script>
29+
<!-- <script src="js/rest_operator.js"></script> -->
30+
<!-- <script src="js/spread_operator.js"></script> -->
31+
<!-- <script src="js/objectdestructing.js"></script> -->
2732
<!-- <script src="js/arraydestructing.js"></script> -->
2833
<!-- <script src="js/templeteLiteral.js"></script> -->
2934
<!-- <script src="js/arrowFunction.js"></script> -->

22. ES6/js/rest_operator.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Rest Operator
2+
// In JavaScript, the rest operator (...) allows you to represent an indefinite number of arguments as an array.
3+
4+
numbers = [10, 20, 30, 40, 50, 60];
5+
6+
var [num1, num2, num3] = numbers;
7+
console.log(num1, num2, num3);
8+
9+
var [num1, num2, ...num3] = numbers;
10+
console.log(num1, num2, num3);
11+
12+
13+
// Use with Object
14+
let person = {
15+
name: 'John',
16+
age: 34,
17+
dob : '19-09-1998'
18+
}
19+
20+
let {name, ...age} = person;
21+
console.log(name, age);
22+
23+
// Use with function
24+
25+
function sum(name, num2, ...num3) {
26+
console.log(name, num2, num3);
27+
}
28+
29+
sum("Abdullah", 10,30,34,33);
30+
// When we pass the arguments numbers function num3 parameters make an array to store the array
31+
sum(5, 7, numbers);
32+
// When we pass the arguments spread the array the when passed it separted to multiple numbers and ...num3 make it as an array and receive it as an array Like
33+
// ... make it array to indvidual item like [numbers = 10,20,30,40,50,60]
34+
// sum function receives it as an array in ...num3 parameters
35+
36+
sum(5, 7, ...numbers);

22. ES6/js/spread_operator.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Spread Operator
2+
// The JavaScript spread operator (...) allows us to quickly copy all or part of an existing array or object into another array or object.
3+
// Split the array element or object make it to array or object or add array with another array
4+
5+
let name = "Abu Huraria";
6+
name = [...name];
7+
console.log(name);
8+
9+
name = {...name};
10+
console.log(name);
11+
12+
13+
// Concat Array
14+
let fruits1 = ["Apple", "Banana", "Cherry", "Date"];
15+
let fruits2 = ["Emblic", "fig", "Guava"];
16+
let fruits3 = ["Hog Plum", "Imbe"];
17+
let fruits4 = ["Jackfruit"];
18+
19+
// It make array to two D arrays
20+
let fruits = [fruits1, fruits2, fruits3, fruits4];
21+
console.log(fruits);
22+
23+
// but we need make arrays to concat with one
24+
fruits = [...fruits1, ...fruits2, ...fruits3, ...fruits4];
25+
console.log(fruits);
26+
27+
// spread Object
28+
let person = {
29+
name: "Abdullah",
30+
occupation: "Student"
31+
}
32+
33+
let person1 = {
34+
name: "Abdur Rahman",
35+
occupation: "Student"
36+
}
37+
38+
39+
let p3 = {...person1, dob:"22/12/1998"};
40+
console.log(p3);
41+
42+
43+
// Spread in function
44+
let numbers = [1, 2, 30, 4, 5, 6, 7];
45+
// console.log(Math.max(numbers)); // none because it dose not work on array
46+
console.log(Math.max(...numbers));
47+
48+
let fullName = ["Abdullah", "Abdur Rahman"];
49+
50+
function fname(fname, lname){
51+
console.log("Name:", fname, lname);
52+
}
53+
54+
55+
fname(...fullName);

22. ES6/js/symbol_op.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Symbol Operator
2+
// In JavaScript, the Symbol is a built-in primitive data type introduced in ES6 (ECMAScript 2015). It represents a unique and immutable identifier, often used for creating properties on objects that are guaranteed to be unique, even if they have the same name as other properties.
3+
4+
// Each symbol created using Symbol() is guaranteed to be unique. Even if two symbols have the same description, they are not the same.
5+
6+
let sym1 = Symbol('foo');
7+
let sym2 = Symbol('foo');
8+
9+
console.log(sym1 === sym2); // false
10+
11+
// Symbols as Object Properties
12+
// Symbols are often used as unique property keys for objects. This prevents accidental overwriting of properties, as symbols are always unique.
13+
14+
// Use Cases for Symbols
15+
// Unique Object Properties: You can use symbols to ensure that property keys are unique and won’t conflict with other properties.
16+
17+
// Avoid Property Name Collisions: Symbols can prevent name collisions when adding properties to objects, especially when extending third-party libraries or built-in objects.
18+
19+
// 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.
20+
21+
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+
}
29+
let symbols = Object.getOwnPropertySymbols(obj);
30+
console.log(symbols); // [Symbol(hidden)]
31+
console.log(obj[symbols[0]]); // 'secret'

0 commit comments

Comments
 (0)