Skip to content

Instantly share code, notes, and snippets.

@bbrt3
Last active August 11, 2021 17:50
Show Gist options
  • Save bbrt3/2b5c6f5e0ee5efed39ad32138dc2731b to your computer and use it in GitHub Desktop.
Save bbrt3/2b5c6f5e0ee5efed39ad32138dc2731b to your computer and use it in GitHub Desktop.
JavaScript
/*
==
It converts the variable values to the same type
before performing comparsion (TYPE COERCION).
It means that if two values are not the same type,
== will convert them to the same type and return true.
===
It doesn't convert the variable values to the same type
before performing comparsion (TYPE COERCION).
It means that if two values are not the same type,
=== will return false.
*/
/*
IIFE (Immediately Invoked Function Expression) is a JS function
that runs as soon as it is defined.
It is based on a design pattern known as a
Self-Executing Anonymous Function and contains two major parts:
a) anonymous function with lexical scope enclosed within the ()
b) Creating immedietialy invoked function expression () through which
JS engine will directly interpret the function
*/
// THIS IS EXACTLY LIKE C# ANONYMOUS FUNCTIONS!!
// REGULAR FUNCTION
let add = (function(a, b) {
return a + b;
});
// EXECUTED IIFE
let add = (function(a,b){
return a + b;
})(10, 20);
/*
null
It is a special value meaning "no value"
It is a special object because typeof null returns object.
- empty / non-existent value
- must be assigned
int a = null; <= null
undefined
- variable has been declared but not defined
int a; <= undefined
*/
/*
Promise can be RESOLVED (passes successful result to callback function)
or REJECTED (passess reason why promise was rejected to other callback function)
A promise represents an operation that hasn't completed yet.
ASYNC ONES!
*/
updateSchedule(empID: number): Promise<string> {
return new Promise((resolve, reject) => {
let result: string = this.processCalendar();
if (result === 'success') {
resolve('Done updating schedule');
} else {
reject('Unable to update schedule');
}
};
}
ngOnInit() {
this.dataService.updateSchedule(10)
.then(
data => console.log(`Resolved: ${data}`),
reason => console.log(`Rejected: ${reason}`)
)
.catch(
err => console.log(`Error: ${err}`)
)
}
// try-catcher
ngOnInit() {
this.getAuthorRecommendationAsync(1)
.catch(err => this.loggerService.error(err));
}
private async getAuthorRecommendationAsync(readerID: number): Promise<void> {
let author: string = await this.dataService.getAuthorRecommendation(readerID);
this.loggerService.log(author);
}
/*
To provide inheritance, objects can have a prototype object,
which acts as a template object that it inherits methods and
properties from.
An object's prototype may also have a prototype object, which it
inherits stuff, and so on.
This is often reffered to as a prototype chain, and explains why different
objects have properties and meethods defined on other obejcts available to them.
All JS objects inherit properties and methods from a prototype:
Date object inherits from Date.prototype
Array objects inherit from Array.prototype
till null!
*/
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
// adding new property to object constructor
Person.prototype.nationality = "English";
// can also add methods like that!!
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
var person = new Person("a", "b", 11, "b");
person.name();
// PROTOTYPES ARE THE MECHANISM BY WHICH JS OBJECTS
// INHERIT FEATURES FROM ONE ANOTHER
var a = ["yo", "whadup", "?"];
// Tablice dziedziczą z Array.prototype
// (który zawiera metody takie jak indexOf, forEach, itd.)
// Łańcuch prototypów wygląda następująco:
// a ---> Array.prototype ---> Object.prototype ---> null
/*
VAR
- function/locally scoped
- globally scoped when outside of functions
- can override it with variable of the same name
- can be updated, can be re-declared
LET
- block scoped
- can be updated, can't be re-declared
CONST
- block scoped
- const variables maintain constant values
- cannot be updated, cannot be re-declared
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment