Grouping Functionality JavaScript
One way to do it and we do it early on in learning JavaScript or maybe any programming language, is defining a Class.
class Car {
}
lets say. And then inside the class we have
constructor(name, year){
this.name = name
this.year = year
}
What do I need to do with Car in order to use it? Car is useless until I do what?
Instantiate it. Create a name in memory, with which the blueprint has meaning, and can be used according to the rules of JavaScript.
const honda = new Car(“Honda”, 1977).
Now honda has all the functionality I “blueprinted” in the Car class.
Another example:
class MenuItem {
constructor(name, price){
this.name = name
this.price = price
}
}
const omelette = new MenuItem(“Omelette”, 5.99)
—
Coding becomes more fluid for me when I think of it as all I’m doing is creating names and giving those names functionality according to the rules of the language.
Sometimes when addressing an error,
- I’ll ask myself ok, the error links to what name in memory, so I’m just looking for a word…then,
- What’s the functionality I assigned to that word…then,
- What are the rules of the language re: that functionality.
- At that point, I’ll close my laptop screen and think about it without the internet at my fingertips.
- Think about the error message, the rules of the language…without screen hopping etc.