Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f757d6

Browse files
add code examples for chapter 4
1 parent 11d6544 commit 8f757d6

File tree

16 files changed

+135
-0
lines changed

16 files changed

+135
-0
lines changed

‎JS/04-Objects/Functions/IIFE.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Resources:
3+
* 1. https://flaviocopes.com/javascript-iife/
4+
* 2. https://stackabuse.com/javascripts-immediately-invoked-function-expressions/
5+
* 3. https://medium.com/@vvkchandra/essential-javascript-mastering-immediately-invoked-function-expressions-67791338ddc6
6+
*/
7+
8+
9+
10+
( // open IIFE
11+
// Inner Anonymous Function
12+
function () {
13+
var tmp = "something";
14+
console.log(tmp);
15+
return tmp;
16+
}
17+
() // p
18+
); // close IIFE
19+
20+
21+
/**
22+
** Passing Data
23+
*/
24+
( // open IIFE
25+
26+
// Inner Anonymous Function
27+
function (data) {
28+
29+
var tmp = data; // not a global variable
30+
console.log(tmp);
31+
}
32+
("something else")
33+
); // close IIFE
34+
35+
// Arrow function
36+
(() => {
37+
/* */
38+
})()
39+
40+
41+
/**
42+
* with unary operators
43+
*/
44+
45+
+function () {
46+
// Code that runs in your function
47+
console.log("+");
48+
}();
49+
50+
-function () {
51+
// Code that runs in your function
52+
console.log("-");
53+
}();
54+
55+
!function () {
56+
console.log("!");
57+
// Code that runs in your function
58+
}();
59+
60+
~function () {
61+
// Code that runs in your function
62+
console.log("~");
63+
}();
64+
65+
void function () {
66+
// Code that runs in your function
67+
console.log("void");
68+
}();
69+
70+
71+
72+
for (var i = 1; i <= 5; i++) {
73+
(function (step) {
74+
setTimeout(
75+
() => console.log(`I reached step ${step}`),
76+
100 * i
77+
);
78+
})(i);
79+
}

‎JS/04-Objects/Functions/anon.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function (param1, param2) {
2+
return param1 + param2;
3+
};
4+
console.log("something")

‎JS/04-Objects/Functions/arrow.png

113 KB
Loading[フレーム]

‎JS/04-Objects/Functions/arrows.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let singleParam = parameter => console.log(parameter);
2+
let multiParam = (parameter,parameter2) => console.log(parameter,parameter2);
3+
let mutlilineArrow = (parameters)=>{
4+
console.log(parameters)
5+
};
6+

‎JS/04-Objects/Functions/closures.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
function closureScoped(job) {
4+
if (job=="designer") {
5+
return (name)=>console.log(`my name is ${name} my job is ${job}`)
6+
}
7+
else if (job=="teacher") {
8+
return (name)=>console.log(`my name is ${name} my job is ${job}`)
9+
}
10+
else {
11+
return (name)=>console.log(`my name is ${name} my job is ${job}`)
12+
}
13+
}
14+
closureScoped("designer")("hans");
15+
16+
function curryingClosure(job){
17+
return function(name) {
18+
if (job=="Teacher")
19+
console.log(` ${name} can you explain closures? ${job}`)
20+
else if (job=="javaScripter")
21+
console.log(`${name} can explain closure and currying because he is a ${job}`)
22+
else
23+
console.log(`${name} what is your malfunction?`)
24+
}
25+
}
26+
27+
curryingClosure("javaScripter")("hans")

‎JS/04-Objects/Functions/closures.png

164 KB
Loading[フレーム]

‎JS/04-Objects/Functions/declaration.png

59.1 KB
Loading[フレーム]
131 KB
Loading[フレーム]
582 KB
Loading[フレーム]

‎JS/04-Objects/Functions/return.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getRectArea(width, height) {
2+
if (width > 0 && height > 0) {
3+
console.log(width * height)
4+
return width * height;
5+
}
6+
console.log(0)
7+
return 0;
8+
}
9+
10+
11+
getRectArea(3, 4) // 12
12+
getRectArea(-3, 4) // 0
13+
// console.log(getRectArea(3, 4)); // 12
14+
// console.log(getRectArea(-3, 4)); // 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /