scrimba
node.js design patterns
001 - introduction
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

001 - introduction
AboutCommentsNotes
001 - introduction
Expand for more info
index.ts
run
preview
console
// source code https://github.com/PacktPublishing/Node.js_Design_Patterns_Second_Edition_Code/tree/master/Chapter01

/* 1
console.log(x);
// */

/* 2
if (false) {
var x = "hello";
}
console.log(x);
// */

/* 3
if (true) {
var x = "hello";
}
console.log(x);
// */

/* 4 reference error
if (false) {
let x = "hello";
}
console.log(x);
// */

/* 5
const numbers = [2, 6, 7, 8, 1];
const even = numbers.filter(x => {
if (x % 2 === 0) {
console.log(x + ' is even!');
return true;
}
});
// */



/* 6 ReferenceError
for (let i=0; i < 10; i++) {
// do something here
}
console.log(i);
// */

/*
const x = 'This will never change';
x = '...';

console.log(x)
// */

/*
const x = {};
x.name = 'John';
// x = null; // This will fail
// */

/*
function DelayedGreeter(name) {
this.name = name;
}
DelayedGreeter.prototype.greet = function () {
setTimeout(function cb() {
console.log('Hello ' + this.name);
}, 500);
};
const greeter = new DelayedGreeter('World');
// console.log('hello ' + greeter.name)
greeter.greet(); // will print "Hello undefined"
// */

/*
function DelayedGreeter(name) {
this.name = name;
}
DelayedGreeter.prototype.greet = function () {
setTimeout((function cb() {
console.log('Hello ' + this.name);
}).bind(this), 500);
};
const greeter = new DelayedGreeter('World');
greeter.greet(); // will print "Hello undefined"
// */

/*
var module = {
x: 42,
getX: function () {
return this.x;
}
}

var unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined

var boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42
// */

/* lexical scope of the arrow function is a closure
// see https://medium.com/@nickbalestra/javascripts-lexical-scope-hoisting-and-closures-without-mystery-c2324681d4be
function DelayedGreeter(name) {
this.name = name;
}
DelayedGreeter.prototype.greet = function () {
setTimeout(() => console.log('Hello ' + this.name), 500);
};

const greeter = new DelayedGreeter('World');
greeter.greet(); // will print "Hello undefined"
// */

/* prototype-based
function Person(name, surname, age) {
this.name = name;
this.surname = surname;
this.age = age;
}
Person.prototype.getFullName = function () {
return this.name + '' + this.surname;
};
Person.older = function (person1, person2) {
return (person1.age >= person2.age) ? person1 : person2;
};
// */

/* syntactic sugar
class Person {
name;
surname;
age;
constructor(name, surname, age) {
this.name = name;
this.surname = surname;
this.age = age;
}
getFullName() {
return this.name + ' ' + this.surname;
}
static older(person1, person2) {
return (person1.age >= person2.age) ? person1 : person2;
}
}
// */

/*
class PersonWithMiddlename extends Person {
constructor(name, middlename, surname, age) {
super(name, surname, age);
this.middlename = middlename;
}
getFullName() {
return this.name + '' + this.middlename + '' + this.surname;
}
}
// */


/*
const person = {
name: 'George',
surname: 'Boole',
get fullname() {
return this.name + '' + this.surname;
},
set fullname(fullname) {
let parts = fullname.split(' ');
this.name = parts[0];
this.surname = parts[1];
}
};
console.log(person.fullname); // "George Boole"
console.log(person.fullname = 'Alan Turing'); // "Alan Turing"
console.log(person.name); // "Alan"
// */


/*
class Person2 {
name = 'George';
surname = 'Boole';
getFullname() {
return this.name + '' + this.surname;
};
setFullname(fullname: String) {
let parts = fullname.split(' ');
this.name = parts[0];
this.surname = parts[1];
}
};
let person2 = new Person2;
console.log(person2.getFullname); // "George Boole"
console.log(person2.setFullname('Alan Turing')); // "Alan Turing"
console.log(person2.name); // "Alan"
// */

/*
const profiles = new Map();

profiles.set('twitter', '@adalovelace');
profiles.set('facebook', 'adalovelace');
profiles.set('googleplus', 'ada');
console.log(profiles.size); // 3
profiles.has('twitter'); // true
profiles.get('twitter'); // "@adalovelace"
profiles.has('youtube'); // false
profiles.delete('facebook');
profiles.has('facebook'); // false
profiles.get('facebook'); // undefined

for (const entry of profiles) {
console.log(entry);
}

console.log(profiles)
// */

/* Map
const tests = new Map();
tests.set(() => 2 + 2, 4);
tests.set(() => 2 * 2, 4);
tests.set(() => 2 / 2, 1);
for (const entry of tests) {
console.log((entry[0]() === entry[1]) ? 'PASS' : 'FAIL');
}
// */

/* Set
const s = new Set([0, 1, 2, 3]);
s.add(3); // will not be added
s.size; // 4
s.delete(0);
s.has(0); // false
for (const entry of s) {
console.log(entry);
}

// */

/* WeakMap
let obj = {};
const map = new WeakMap();
map.set(obj, {key: "some_value"});
console.log(map.get(obj)); // {key: "some_value"}
obj = undefined; // now obj and the associated data in the map, and will be cleaned up in the next gc cycle
// */

/* WeakSet
let obj1= {key: "val1"};
let obj2= {key: "val2"};
const set= new WeakSet([obj1, obj2]);
console.log(set.has(obj1)); // true
obj1= undefined; // now obj1 will be removed from the set
console.log(set.has(obj1)); // false
// */

/*
let obj1= {key: "val1"};
let obj2= {key: "val2"};
const set= new Set([obj1, obj2]);
console.log(set.has(obj1)); // true
obj1= undefined; // now obj1 will be removed from the set
console.log(set.has(obj1)); // false
// */

//* Template literals
const name = "Leonardo";
const interests = ["arts", "architecture", "science", "music", "mathematics"];
const birth = { year: 1452, place: 'Florence' };
const text = `${name} was an Italian polymath
interested in many topics such as
${interests.join(', ')}.He was born
in ${birth.year} in ${birth.place}.`;
console.log(text);
// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

//*

// */

Console
"Leonardo was an Italian polymath interested in many topics such as arts, architecture, science, music, mathematics.He was born in 1452 in Florence."
,
index.html
-4:41