Constructor Functions
- The
new
keyword - Pseudoclassical Instantiation
- when you invoke a function using the
new
keyword, these two lines are done for you implicitly (“under the hood”) and the object that is created is called this
.
- If you leave off
new
when you invoke the function, that this
object never gets created nor does it get implicitly returned.
// ES5
function Animal (name, energy) {
// const this = Object.create(Animal.prototype)
this.name = name
this.energy = energy
function
// return this
}
const leo = new Animal('Leo', 7)
const snoop = new Animal('Snoop', 10)
- Function Prototype
prototype
is just a property that every function in JavaScript has and it allows us to share methods across all instances of a function.
// ES5
function Animal (name, energy) {
this.name = name
this.energy = energy
}
Animal.prototype.eat = function (amount) {
console.log(`${this.name} is eating.`)
this.energy += amount
}
Animal.prototype.sleep = function (length) {
console.log(`${this.name} is sleeping.`)
this.energy += length
}
Animal.prototype.play = function (length) {
console.log(`${this.name} is playing.`)
this.energy -= length
}
const leo = new Animal('Leo', 7)
const snoop = new Animal('Snoop', 10)
- Static Methods
- Whenever you have a method that is specific to a class itself, but doesn’t need to be shared across instances of that class, you can add it as a static property of the class.
- With ES5, this same pattern is as simple as just manually adding the method to the function object.
function Animal (name, energy) {
this.name = name
this.energy = energy
}
Animal.prototype.eat = function (amount) {
console.log(`${this.name} is eating.`)
this.energy += amount
}
Animal.prototype.sleep = function (length) {
console.log(`${this.name} is sleeping.`)
this.energy += length
}
Animal.prototype.play = function (length) {
console.log(`${this.name} is playing.`)
this.energy -= length
}
Animal.nextToEat = function (nextToEat) {
const sortedByLeastEnergy = animals.sort((a,b) => {
return a.energy - b.energy
})
return sortedByLeastEnergy[0].name
}
const leo = new Animal('Leo', 7)
const snoop = new Animal('Snoop', 10)
console.log(Animal.nextToEat([leo, snoop])) // Leo
- Getting the prototype of an object
Object.getPrototypeOf()
method
const prototype = Object.getPrototypeOf(leo)
console.log(prototype)
// {constructor: ƒ, eat: ƒ, sleep: ƒ, play: ƒ}
prototype === Animal.prototype // true
console.log(leo.constructor) // Logs the constructor function
- Determining if a property lives on the prototype
- There are certain cases where you need to know if a property lives on the instance itself or if it lives on the prototype the object delegates to.
hasOwnProperty()
hasOwnProperty
is a property on every object that returns a boolean indicating whether the object has the specified property as its own property rather than on the prototype the object delegates to.
const leo = new Animal('Leo', 7)
for(let key in leo) {
if (leo.hasOwnProperty(key)) {
console.log(`Key: ${key}. Value: ${leo[key]}`)
}
}
// what we see are only the properties that are on the leo object itself
// rather than on the prototype leo delegates to as well.
- Check if an object is an instance of a Class
object instanceof Class
- The way that instanceof works is it checks for the presence of constructor.prototype in the object’s prototype chain. In the example below, leo instanceof Animal is true because Object.getPrototypeOf(leo) === Animal.prototype.
function Animal (name, energy) {
this.name = name
this.energy = energy
}
function User () {}
const leo = new Animal('Leo', 7)
leo instanceof Animal // true
leo instanceof User // false