- Monday: REST API
- TUESDAY - WEDNESDAY: on sick leave
- Nightmarejs
- Keyword ''this" - 4 cases to define "this" context
- Global object: when "this" is outside a declared object -> refers to the global obj which is usually window object in a browser
E.g. when a global var is declared -> it is actually attached to the global window
var person = "Elliot";
window.person // "Elliot"
this.person // "Elliot"
function heyThis () {
console.log(this);
}
heyThis(); // window object
- Global with strict: in "use strict" mode, "this" inside a function does not automatically refers to the window object anymore but is undefined
- Implicit/ Object: "this" inside a declared object refers to the closet parent object
const person = {
name : "Jack",
sayHi : function () {
console.log(`Hi ${this.name} !!!!!`);
},
thisContext: function () {
console.log(this === person);
},
person2 : {
sayHello: function () {
console.log(`Hello ${this.name}`);
},
thisContext: function () {
console.log(this === person);
}
}
}
person.sayHi(); // Hi Jack !!!!!
person.thisContext(); // true
person.person2.sayHello(); // Hello undefined // this refers to person2 obj now
person.person2.thisContext(); // false
- Explicit Binding: Set ''this" context by call, apply or bind
Call and apply are just different in ways of passing parameters. They are both invoked immediately
const Jack = {
name: "Jack",
sayHi: function() {
return `Hi ${this.name}`
}
cal: function(a, b, c, d) {
return this.name + " counts " + a + b + c + d
}
}
const Jane = {
name: "Jane"
}
Jack.sayHi(); // Hi Jack
Jack.sayHi.call(Jane); // Hi Jane
Jack.cal(1, 2, 3, 4); //Jack counts 1234
Jack.cal.call(Jane, 1, 2, 3, 4); // Jane counts 1234
Jack.cal.apply(Jane, [1, 2, 3, 4]); // Jane counts 1234
// Bind
// Returns the function definition but not invoke the function immediately
const John = {
name: "John"
}
const JohnCount = Jack.cal.bind(John, 1, 2, 3, 4);
JohnCount() // John counts 1234
//
const JohnCount = Jack.cal.bind(John, 1, 2);
JohnCount(3, 4) // John counts 1234
- "New" keyword: when we create a new object with "new" keyword, the context of "this" is firstly set to an empty object and returns the newly created object with "new"
function Person(firstname, lastname) {
this.firstname = firstname
this.lastname = lastname
}
const Jack = new Person ("Jack", "Daniel");
Jack.firstname // Jack
Jack.lastname // Daniel
OOP: OBJECT ORIENTED PROGRAMMING - A MODEL BASED AROUND OBJECTS AND BLUEPRINTS THAT ARE USED TO CREATE OBJECTS
These blueprints are CLASSES - objects created from classes are instances
In JS, we can use Constructor Functions to build the blueprint
function House(bedroom, bathroom, floor) {
this.bedroom = bedroom
this.bathroom = bathroom
this.floor = floor
}
const myHouse = new House(3, 2, 4);
myHouse.bedroom // 3
myHouse.bathroom // 2
Multiple constructors
function Car(color, model, year) {
this.color = color
this.model = model
this.year = year
// preset properties
this.wheel = 4
}
function Bike(color, model, year) {
Car.call(this, color, model, year)
// or
Car.apply(this, arguments) // arguments = [color, model, year]
// preset properties
this.wheel = 2
}
"new" keyword: first create an empty object -> create a link between the object and the prototype object of the constructor function via __proto__ . The prototype object also has a .constructor linking back to the constructor and the constructor has. a .prototype
Closures: inner functions that use variable from outer functions that are already returned -> create private variables
- THURSDAY: POSTGRESQL
CREATE ROLE linh CREATEDB LOGIN;
ALTER ROLE linh ENCRYPTED PASSWORD '123'
pgcli -h localhost -p 5432 -U linh -W // connect to postgresql as role linh
// create a new db
CREATE DATABASE linhdb OWNER linh
// create a schme
CREATE SCHEMA "cms"
// show current schema
SHOW search_path
// change schema
SET SCHEMA 'cms'
// show current user/role
SELECT current_user
In postgres, postgres user is superuser. Other created users can have different levels of authorities and access to different databases. Schema 'public' is public in a way that other users can enter another database owned by other users and create tables in 'public' schema. However, they are still not allowed to see and select data from database created by others despite these databases are in 'public' schema.
PG-PROMISE: Connect Nodejs and Postgresql