Misc Notes
Misc Notes
Misc
Comparison
Classes
Prototype
Maps
Iterators
Generator
Misc
operator typeof
String templating works by using backticks (`) around the text and adding variables in ${}, i.e.,
`Hello ${name}`
switch([condition]) {
case 1: ...; break;
case 2: ...; break;
default: ...; break;
}
Closures:
const add = (function () {
let counter = 0;
return function () {
counter += 1;
return counter
}
console.log(`counter is now ${counter}`)
})();
add();
add();
// counter is now 2
value[x] x is a string that will be evaluated into the name of the property
Rest parameters: one can use the ... notation to represent the rest of values passed being
grouped into a variable, and it can also be used to explode the amount of items into parameters for
a function
function resting1(...args) {
console.log(`>${args}`);
}
resting1([1,3,4,7]);
// >1,3,4,7
Comparison
Referential equality: Determines whether the two provided operands refer to the same
reference/object instance
Deep equality: Determines whether objects are equal by comparing each property in the operands
== equal to
Always use strict equality (value === null) for reliable null checks.
Classes
class Rabbit {
constructor(type) {
this.type = type;
}
#speak(line) { // # denotes private method/property
console.log(`The ${this.type} rabbit says '${line}'`);
}
sspeak(line) {
this.#speak(line)
}
}
Prototype
Prototypes are a way for JS to enforce a type hierarchy in which all objects are eventually derived from
Object , and share all attributes and methods inherited along the way. The atts and meths belong to
the prototype, not to the class itself, which is a good optimization.
There are several highest-level prototypes, such as Object , Function and Array
let protoRabbit = {
speak(line) {
console.log(`The ${this.type} rabbit says '${line}'`);
}
};
let blackRabbit = Object.create(protoRabbit);
blackRabbit.type = "black";
blackRabbit.speak("I am fear and darkness");
// → The black rabbit says 'I am fear and darkness'
The “proto” rabbit acts as a container for the properties that are shared by all
rabbits. An individual rabbit object, like the black rabbit, contains properties
that apply only to itself—in this case its type—and derives shared properties
from its prototype.
Maps
This is an object, not a map. It will have properties derived from its prototype.
let ages = {
Boris: 39,
Liang: 22,
Júlia: 62
};
console.log(`Júlia is ${ages.get("Júlia")}`);
// → Júlia is 62
console.log(ages.has("toString"));
// → false
The methods set , get , has and has are part of the interface of the Map object.
getters, setters and static
class Temperature {
constructor(celsius) {
this.celsius = celsius;
}
get fahrenheit() {
return this.celsius * 1.8 + 32;
}
set fahrenheit(value) {
this.celsius = (value - 32) / 1.8;
}
static fromFahrenheit(value) {
return new Temperature((value - 32) / 1.8);
}
}
Iterators
const rangeIterator = {
next() {
let result;
if (nextIndex < end) {
result = { value: nextIndex, done: false };
nextIndex += step;
iterationCount++;
return result;
}
return { value: iterationCount, done: true };
},
};
return rangeIterator;
}
Generator
console.log(/abc/.test("abcde"));
// → true
function jorker(jork) {
console.log(`[${jork && '&& JORK'}]`);
console.log(`[${jork || '|| JORK'}]`);
console.log(`[${jork ?? '?? JORK'}]`);
console.log('\n');
}
code output
[undefined]
jorker(); [|| JORK]
[?? JORK]
[]
jorker(''); [|| JORK]
[]
[&& JORK]
jorker('00'); [00]
[00]
[null]
jorker(null); [|| JORK]
[?? JORK]
[undefined]
jorker(undefined);
[