Skip to content

Commit d4a52af

Browse files
committed
Delete file 01_everything.js and split out into new files
This commit deletes the file 01_everything.js which demonstrated various types of functions in JavaScript and adds various newer files for different types of functions in js.
1 parent 7a0b660 commit d4a52af

File tree

9 files changed

+61
-67
lines changed

9 files changed

+61
-67
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Function Expression
2+
const functionExpression = function () {
3+
console.log("Function Expression");
4+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Arrow Function
2+
const arrowFunction = () => {
3+
console.log("Arrow Function");
4+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Named Function Expression
2+
const namedFunctionExpression = function namedFunc() {
3+
console.log("Named Function Expression Type");
4+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Generator Function
2+
function* generatorFunctionType() {
3+
yield 1;
4+
yield 2;
5+
yield 3;
6+
}

Tutorials/Basic/functions/01F_IIFE.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Immediately Invoked Function Expression (IIFE)
2+
(function () {
3+
console.log("IIFE Function Type");
4+
})();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Recursive Function
2+
function countdown(num) {
3+
if (num > 0) {
4+
console.log(num);
5+
countdown(num - 1);
6+
}
7+
}
Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// 00_everything.js
2-
1+
// 01_functions.js
32
/*
43
This JavaScript file demonstrates various types of functions in JavaScript, including regular, function expression, arrow function, and named function expressions.
54
@@ -22,61 +21,4 @@ function regularFunction() {
2221
console.log("Regular Function");
2322
}
2423

25-
// Function Expression
26-
const functionExpression = function() {
27-
console.log("Function Expression");
28-
};
29-
30-
// Arrow Function
31-
const arrowFunction = () => {
32-
console.log("Arrow Function");
33-
};
34-
35-
// Regular Function Type
36-
function regularFunctionType() {
37-
console.log("Regular Function Type");
38-
}
39-
40-
// Function Expression Type
41-
const functionExpressionType = function() {
42-
console.log("Function Expression Type");
43-
};
44-
45-
// Arrow Function Type
46-
const arrowFunctionType = () => {
47-
console.log("Arrow Function Type");
48-
};
49-
50-
// Immediately Invoked Function Expression (IIFE)
51-
(function() {
52-
console.log("IIFE Function Type");
53-
})();
54-
55-
// Generator Function
56-
function* generatorFunctionType() {
57-
yield 1;
58-
yield 2;
59-
yield 3;
60-
}
61-
62-
// Named Function Expression
63-
const namedFunctionExpression = function namedFunc() {
64-
console.log("Named Function Expression Type");
65-
};
66-
67-
// Recursive Function
68-
function countdown(num) {
69-
if (num > 0) {
70-
console.log(num);
71-
countdown(num - 1);
72-
}
73-
}
74-
75-
regularFunction();
76-
functionExpression();
77-
arrowFunction();
78-
regularFunctionType();
79-
functionExpressionType();
80-
arrowFunctionType();
81-
countdown(5);
82-
24+
regularFunction()

Tutorials/Basic/functions/04_functions.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// freezing_objects.js
2+
3+
// "use strict"; directive is necessary to use Object.freeze()
4+
"use strict";
5+
6+
// Let's create a simple object
7+
const myObject = {
8+
dob: 1990 // My D.O.B
9+
}
10+
11+
// But we can change it
12+
myObject.dob = 2000 // Changed my D.O.B
13+
14+
// Object.freeze(obj) makes the object and its properties
15+
// read-only. It cannot be changed, added, or deleted.
16+
// Any attempt to do so will result in an error.
17+
18+
// However, Object.freeze() only works in "use strict"; mode.
19+
// Without it, the code will execute without any errors
20+
21+
// Let's try to change the object and see what happens
22+
Object.freeze(myObject) // Object cannot be extended
23+
24+
try {
25+
myObject.dob = 2200 // Error: Cannot assign to read-only property 'dob'
26+
} catch (error) {
27+
console.log(error) // TypeError: Cannot assign to read-only property 'dob'
28+
// Note: This error will not be caught in non-strict mode
29+
}
30+

0 commit comments

Comments
 (0)