-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsConcepts.html
81 lines (67 loc) · 2.86 KB
/
JsConcepts.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<html>
<body>
<script>
/*
JAVASCRIPT DEMO
Closures
Functional programming - https://medium.freecodecamp.org/functional-programming-in-js-with-practical-examples-part-1-87c2b0dbc276
Function composition
Prototype chain
*/
/* Tools */
var Logger = function () { }
Logger.prototype.constructor = Logger;
Logger.prototype.log = function (message) { }
function getBodyLogger() {
function bodyLogger() { }
bodyLogger.prototype = new Logger();
bodyLogger.prototype.log = function (message) {
document.body.innerHTML += message + '<br/>';
}
return new bodyLogger();
}
function getConsoleLogger() {
function consoleLogger() { }
consoleLogger.prototype = new Logger();
consoleLogger.prototype.log = function (message) {
console.log(message);
}
}
var logger = getBodyLogger();
/*-----------------*/
logger.log('--- Closures example ---')
var closureFunction = contextFunction(logger);
closureFunction();
function contextFunction(logger) {
var internalVariable = 'a private variable';
function internalFunction() {
if (Logger.prototype.isPrototypeOf(logger))
logger.log('this is an intenal function showing ' + internalVariable);
}
return internalFunction;
}
/*-----------------*/
/*-----------------*/
logger.log('<br/>--- Function composition example ---')
functionComposition(logger);
function functionComposition(logger) {
const tag = tagType => tagContent => '<' + tagType + '>' + tagContent + '</' + tagType + '>';
var ul = tag('ul');
var li = tag('li');
var liItems = items => items.map(li).join('');
var boldText = tag('b')('this is bold!');
var hyperlink = tag('a href="https://www.github.com/fcnatra"')('go to GitHub');
var list = ul(liItems(['item1', 'item2']));
if (Logger.prototype.isPrototypeOf(logger)) {
logger.log(boldText);
logger.log(hyperlink);
logger.log(list);
}
}
/*-----------------*/
/*-----------------*/
logger.log('--- Prototype chain ---')
logger.log('Is Logger the prototype of the logger instance?: ' + Logger.prototype.isPrototypeOf(logger));
</script>
</body>
</html>