This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (121 loc) · 3.09 KB
/
script.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Default Parameters
function Person(name = 'Artur', {age = 22, country = 'Portugal'} = {}) {
this.name = name;
this.age = age;
this.country = country;
}
Person.prototype.toString = function() {
// CONST VARIABLE TYPE
const INSTITUTE = 'ISEP';
// Template String
console.log(`${this.name}, age ${this.age} from ${this.country}.
Studying at ${INSTITUTE}`);
};
// Arrow function
let createPersonAndPrint = (name, age, country) => {
// LET VARIABLE TYPE
let instance = new Person(name, {age: age, country: country});
instance.toString();
}
// Very cool usage of default parameters
createPersonAndPrint(undefined, undefined, undefined);
createPersonAndPrint('André');
createPersonAndPrint('Sr. Carreira', 32);
// Rest params
function iterateArray(...iteratable) {
// For ... of
for (let element of iteratable) {
console.log(element);
}
}
iterateArray('ES2015', 'is', 'awesome!');
// Spread operator
let x = 'first', y = 'second', z = 'third';
let xyz = [x, y, z];
function spreadParams(x, y, z) {
console.log(`x: ${x} | y: ${y} | z: ${z}`);
}
spreadParams(...xyz);
// Object initializer shorthand
let shorty = {x, y, z};
console.log(`x: ${shorty.x} | y: ${shorty.y} | z: ${shorty.z}`);
// Method initializer shorthand
function Someone() {
const DIET = 'Vegan';
return {
printDiet() {
console.log(`Diet for Someone is ${DIET}`);
}
};
}
let thisGuy = new Someone();
thisGuy.printDiet();
// Merging properties with Object.assign
function styleMerger(options = {}) {
const defaults = {
display: 'inline-block',
padding: '15px',
top: 'unset'
};
let finalStyle = Object.assign({}, defaults, options);
console.log(finalStyle);
}
styleMerger({padding: '59px'});
// Array destructuring
let [first, , ...rest] = ['start', 'curve', 'straight', 'curve', 'finish'];
console.log(`First: ${first} | Rest: ${rest}`);
// Array.find
let users = [
{name: 'John', admin: false},
{name: 'Jane', admin: true},
{name: 'Jim', admin: true}
];
let admin = users.find(user => user.admin);
console.log(admin);
// Maps
let mapSettings = new Map();
mapSettings.set('user', 'John');
mapSettings.set('second', 'image');
mapSettings.set('last', ['array0', 'array1']);
for (let [key, value] of mapSettings) {
console.log(`${key} - ${value}`);
}
// Sets
let tags = new Set();
tags.add('JavaScript');
tags.add('Programming');
tags.add('Web');
for(let tag of tags){
console.log(`Tag: ${tag}`);
}
console.log(tags.has('Web'));
// Classes
class Advertisement {
constructor(title, link){
this.title = title;
this.link = link;
}
_buildContent(){
return `<h1>${this.title}</h1>
<a href="${this.link}">${this._linkText()}</a>`;
}
_linkText(){
return "Click Here";
}
render(){
return this._buildContent();
}
}
// Here's an example of how this class is used:
/*
let ad = new Advertisement("CodeSchool", "http://codeschool.com");
_appendToPage( targetElement, ad.render() );
*/
class SidebarAdvertisement extends Advertisement {
constructor(title, link) {
super(title, link);
}
_linkText() {
return 'Sign up now!';
}
}