Today I began to review Javascript objects and constructors via my bootcamp and FreeCodeCamp. I am almost halfway through the FreeCodeCamp lessons and hope to finish by tonight or tomorrow morning.
Some notes that I took are:
Constructors are functions that create new objects. They help us to define properties and behaviors that will belong to a new object.
A sample constructor:
function Person(firstName, lastName) {
this.firstName = firstName,
this.lastName = lastName,
this.age = 23
}
let sally = new Person()
The sally
object is now an instance of the Person constructor. You can check this by running:
karen instanceof Person; // => false
sally instanceof Person; // => true
john instanceof Person; // => false
All objects have properties. Own
properties are defined directly on the object instance itself. And prototype
properties are defined on the prototype.
function Person(firstName, lastName) {
this.firstName = firstName; //own property
this.lastName = lastName; //own property
}
Person.prototype.age = 25; // prototype property
let will = new Person("Will, Smith");
I learned a lot about javascript objects and constructors and cant wait to learn more.
In addition, I was able to complete the FizzBuzz problem using Javascript. I plan to start to do more challenges and algorithms as I learn more.
As always, thanks for reading!
Sincerely,
Brittany
Top comments (0)