Anurag Kumar
18th Oct, 2019
- Basics
- Inheritance: Prototype Chains
- Functional Programming
- Collections
- Objects
this
- Async Operations
- ES6+
- References
- Dynamically Typed
- OOP & FP
// A reassignable varibale
let character = 'Ross';
var tvSeries = 'Friends';
// Non reassignable
const rachel = 'Rachel';
- Assignment:
=
,+=
,-=
,*=
, ... - Arithmatic:
+
,-
,*
,/
,%
- Comparison:
==
,!=
,<
,!==
,===
, ... - Logical:
&&
,||
,!
typeof
,instanceof
,in
,delete
,new
Default value trick
let wereOnABreak; // default value is undefined
// let wereOnABreak = undefined; // NEVER do this
if (character === 'Ross') {
wereOnABreak = true;
} else if (character === 'Rachel') {
wereOnABreak = false;
} else {
wereOnABreak = null;
}
Indexed using numbers
const characters = ['Chandler', 'Joey', 'Monica', 'Ross', 'Pheobe', 'Rachel'];
const mixed = ['Friends', 1994, , true, [25, 17], null];
mixed[1]; // 1994
mixed["4"] // [25, 17]
mixed[6]; // undefined
mixed[2]; // undefined
mixed.length === 6; /// true
@[1-4] @[1-9]
const characters = [
'Chandler', 'Joey', 'Monica',
'Ross', 'Pheobe', 'Rachel'
];
for (let i = 0; i < characters.length; i++) {
console.log(characters[i]);
}
for (const character of characters) {
console.log(character);
}
let i = 0;
while (i < characters.length) {
console.log(character[i]);
i++;
}
@[1-8] @[1-4, 10-12] @[1-4, 14-18]
Indexed using string
const joey = {
name: "Joey Tribbianni",
catchphrase: `How you doin'?`,
relationships: ['Angela', 'Kathy', 'Katie', 'Ginger', 'Janine'],
age: 26,
};
joey.age // "How you doin'?"
joey.isGuestStar // undefined // Yup! No error
joey.age = 28
joey.isMainCharacter = true; // Can do this too.
console.log(joey); // joey now has a new property
@[1-6] @[1-9] @[1-13]
const joey = {
name: "Joey Tribbianni",
catchphrase: `How you doin'?`,
relationships: ['Angela', 'Kathy', 'Katie', 'Ginger', 'Janine'],
age: 26,
};
// notice `in` instead of `of` as with Arrays
for (const key in joey) {
// will talk about this during inheritence
if (joey.hasOwnProperty(key)) {
console.log(joey[key]);
}
}
function updateRelationship(character, name) {
character.relationships.push(name);
}
function getLastRelationship(character) {
const relationships = character.relationships;
return relationships[relationships.length - 1];
}
@[1-3] @[5-8]
Function Expressions
// functions too are objects!
const getName = function name(character) {
return character.name;
};
const getName = function(character) {
return character.name;
};
const getName = (character) => {
return character.name;
};
const getName = character => character.name;
const getAge = character => ({ age: character.age });
@[1-4] @[1-9] @[1-12] @[1-14] @[1-16]
@fa[quote-left](A class
encapsulates data and functions that act on those data)
Let's take an example
class Character {
constructor(name, age, catchphrase) {
this.name = name;
this.age = age;
this.catchphrase = catchphrase;
this.relationships = [];
}
updateRelationship(name) {
this.relationships.push(name);
}
getLastRelationship = () =>
this.relationships[this.relationships.length - 1];
}
const joey = new Character('Joey Tribbiani', 26, "How you doin'?");
console.log(joey.getLastRelationship()); // undefined
joey.updateRelationship('Angela');
joey.updateRelationship('Kathy');
console.log(joey.getLastRelationship()); // "Kathy"
@[1] @[1-3] @[1, 5-8]
class Ross extends Character {
constructor(age, catchphrase) {
super('Ross Geller', age, catchphrase);
this.marriedTo = null;
this.divorces = [];
}
updateRelationship(name, isGettingMarried) {
if (isGettingMarried) {
this.marriedTo = name;
}
super.updateRelationship(name);
}
// public class field syntax
marry = name => this.updateRelationship(name, true);
divorce() {
this.divorces.push(this.marriedTo);
this.marriedTo = null;
}
}
@[1-6, 22] @[1, 8-13, 22] @[1, 15-16, 22] @[1, 18-22]
const ross = new Ross(30, "Hi.. :'(");
ross.name; // "Ross Geller"
ross.marry('Carol');
ross.getLastRelationship(); // parent class's method
ross.divorce();
@box[text-orange](There are no classes in JS.)
@snap[fragment] @box[text-orange](They are just syntactic suger.) @snapend
function Character(name, age, catchphrase) {
this.name = name;
this.age = age;
this.catchphrase = catchphrase;
this.relationships = [];
}
Character.prototype.updateRelationship = function(name) {
this.relationships.push(name);
};
Character.prototype.getLastRelationship = () =>
this.relationships[this.relationships.length - 1];
@[1-6] @[1-11]
function Ross(age, catchphrase) {
Character.call(this, "Ross Geller", age, catchphrase);
this.marriedTo = null;
this.divorces = [];
}
Ross.prototype = Object.create(Character.prototype);
Ross.prototype.constructor = Ross;
Ross.prototype.marry = (name) => this.updateRelationship(name, true);
Ross.prototype.updateRelationship = (name, isGettingMarried) => {
if (isGettingMarried) { this.marriedTo = name; }
Character.prototype.updateRelationship.call(this, name);
}
Ross.prototype.divorce = () => {
this.divorces.push(this.marriedTo);
this.marriedTo = null
}
@[1-5] @[1-6] @[1-7] @[1-9] @[1-14] @[1-19]
@li
- Functions as first class citizens
- No Side effects
- Like a math function
- Same input, same output @liend
Object <- Function <- Inbuilt Objects
Object.prototype
Object.prototype.hasOwnProperty()
Object.prototype.constructor
Object.prototype.toString()
Function.prototype.call()
Function.prototype.bind()
Function.prototype.apply()
Function.prototype.name
const joey = {
name: "Joey Tribbianni",
catchphrase: `How you doin'?`,
relationships: ['Angela', 'Kathy', 'Katie', 'Ginger', 'Janine'],
age: 26,
};
Object.is({}, {}) // false
// shallow copy
const joeyCopy = Object.assign({}, joey);
// same as ===, except NaN, -0, +0
Object.is(joey, joeyCopy) // false
for (const key in joey) {
if (joey.hasOwnProperty(key)) {
console.log(joey[key]);
}
}
// Better version
Object.keys(joey) // ['name', 'catchphrase', 'relationships', 'age']
.forEach(key => console.log(joey[key]));
Object.values(joey)
.forEach(value => console.log(value));
Object.entries(joey)
.forEach(([key, value]) => {
console.log(key, value);
});
// shallow copy
const joey = Object.fromEntries(Object.entries(joey));
- .forEach
- .filter
- .map
- .sort
- .reduce
- .from
- .fill
- .find
- .findIndex
- .every
- .some
- .join
- .pop
- .push
- .shift()
- .unshift()
- Unique elements
- other types of keys
- ordered collection
- Template String, Template Tagged String
- Arrow Functions, this binding
- Default values, Rest Parameters, spread operators
- Mixing above patterns for Options Object Pattern [mimicking python's *args, **kwargs]
- String methods
- Template tagged string: eg - html, gql, translations, shell scripts, network requests
- proeprty shorthand
- computed properties
- various import and export syntax
- static, get, set
- MDN
- You Don't Know JS
- What are the differences between parallel, concurrent, and asynchronous programming?
- Asynchronous Python
- What the heck is the event loop anyway?
- Exploring JS
- Eloquent Javascript
- Javascript The Code
- The introduction to Reactive Programming you've been missing
- What is a Closure?
- Classical vs Prototypical Inheritance
- Object Prototypes