Day 26: Object Oriented Programming is Complete!
#100DaysOfCode Challenge

Search for a command to run...
#100DaysOfCode Challenge

No comments yet. Be the first to comment.
How Perseverance Led Me to Data Analytics

An SQL cheat sheet for beginners

My top learning resources and why I like them

A how-to- guide with code snippets and a live demo

My Top 7 Git Commands and How I Use Them

Today I finished the Object Oriented Programming section of the JavaScript course from freeCodeCamp. Today’s challenges focused on how constructors, prototypes, subtypes, supertypes, inheritance, and mixins. You can read my full #100DaysOfCode journey on GitHub.
new operator is needed when calling the constructor such as in the example below. This will create a new object, blueBird with the properties blueBird.name, etc. It can then be accessed and modified just like any other property within an object.function Bird() {
this.name = "Albert";
this.color = "blue";
this.numLegs = 2;
}
let blueBird = new Bird();
function Bird(name, color) {
this.name = name;
this.color = color;
this.numLegs = 2;
}
let cardinal = new Bird("Bruce", "red");
instanceof operator. instanceof allows you to compare an object to a constructor, returning true or false based on whether or not that object was created with the constructor."prototype are shared among ALL instances of" the constructor object and "Since all instances automatically have the properties on the prototype, think of a prototype as a "recipe" for creating objects." For example, Bird.prototype.numLegs = 2; will give the property numLegs the value of 2 for all objects created using the Bird constructor.prototype." For example:function Bird(name) {
this.name = name; //own property
}
Bird.prototype.numLegs = 2; // prototype property
let duck = new Bird("Donald");
constructor property! ... To fix this, whenever a prototype is manually set to a new object, remember to define the constructor property:Bird.prototype = {
constructor: Bird,
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
prototype directly from the constructor function that created it."isPrototypeOf method will show the relationship of the object and the constructor that created itprototype. Also, an object’s prototype itself is an object"“Object.create(obj) creates a new object, and sets obj as the new object's prototype". For example, let duck = Object.create(Animal.prototype); will create duck as an object with the prototype based on the Animal constructor.Bird.prototype = Object.create(Animal.prototype); will create a subtype based on the supertype Animal in which Bird will have all of the properties defined by the Animal constructor.prototype from another object, it also inherits the supertype's constructor property."prototype object from a supertype constructor function can still have its own methods in addition to inherited methods."Bird and an Airplane can fly, but a Bird is not an Airplane and vice versa. So we can create the flyMixin to take any object and give it the fly method:let flyMixin = function(obj) {
obj.fly = function() {
console.log("Flying, wooosh!");
}
};
closure."(function () {
console.log("Chirp, chirp!");
})();
This is an anonymous function expression that executes right away, and outputs Chirp, chirp! immediately.
Note that the function has no name and is not stored in a variable. The two parentheses () at the end of the function expression cause it to be immediately executed or invoked. This pattern is known as an immediately invoked function expression or IIFE."

Photo by Kenny Eliason on Unsplash
Most of the challenges were really easy because they involved copying or rewriting code from the example. Which is okay since the last section (Algorithm Scripting) was rather difficult, but I was hoping for more of challenge than just changing a few words in lines of code. Perhaps that will come in the next section (Functional Programming).
That said, I was delighted to see how constructors and prototypes work in practice. Previously, when I read about them in Eloquent JavaScript I found them a bit confusing on a conceptual level, but I think they're starting to make a bit more sense.
Lastly, I’m feeling especially grateful to my partner who’s been very supportive during this programming journey. On this particular evening, he prepared dinner which gave me more time to study. (-:
For my progress visit the timeline on my freeCodeCamp Profile.
You can read my full #100DaysOfCode journal on GitHub