Coding diary to reflect concepts that I am learning and revisiting
Classical Inheritance vs Prototypical inheritance
- Prototypical inheritance allows you to create a new object and combine methods from other objects
- With classical inheritance you are forced to inherit all the methods pertaining to the parent class whereas prototypical inheritance allows you to choose the methods you would like to inherit
Syntax for Private instance variables
- Private variables are variables that cannot be accessed outside of the class that they are in
- In JavaScript, private variables start with
#
class User {
#votingAge = 18; // here #votingAge is a private instance variable
}
JavaScript- Private variables also have to be defined outside of the constructor but can be used inside the constructor
E.g.
class User {
#votingAge;
constructor() {
this.#votingAge = 18;
}
}
JavaScriptPrivate instance methods
- These are methods that can only be used inside the class
class User {
constructor(age) {
this.age = age;
this.#logAge();
}
#logAge() {
console.log(this.age);
}
}
JavaScriptRecap
The some
callback method
- Returns
true
if at least one of the elements in the array statisfies the condition
Example: Creating a function that returns true if one of the ages in the array is below 18
const agesInTheGroup = ages = {
return ages.some(age => age < 18);
}
JavaScriptRecap of map()
Example of using the map.(callback)
method.
Given an array of different words return an array length each word in the array
/**
* @param {string[]} strings
*/
const wordsToLengthConverter = words => {
return words.map(word => word.length):
}
console.log(wordsToLengthConverter(["Hello", "World", "Bonjour"])); // [5, 5, 7]
JavaScript