Skip to content

Instantly share code, notes, and snippets.

@theonlychase theonlychase/constructor
Last active May 5, 2019

Embed
What would you like to do?
## Constructor Functions
1. The `new` keyword - *Pseudoclassical Instantiation*
1. 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`.
2. If you leave off `new` when you invoke the function, that `this` object never gets created nor does it get implicitly returned.
```javascript
// 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)
```
2. Function Prototype
1. `prototype` is just a property that every function in JavaScript has and it allows us to share methods across all instances of a function.
```javascript
// 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)
```
3. Static Methods
1. 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.
2. With ES5, this same pattern is as simple as just manually adding the method to the function object.
```javascript
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
```
1. Getting the prototype of an object
1. `Object.getPrototypeOf()` method
```javascript
const prototype = Object.getPrototypeOf(leo)
console.log(prototype)
// {constructor: ƒ, eat: ƒ, sleep: ƒ, play: ƒ}
prototype === Animal.prototype // true
console.log(leo.constructor) // Logs the constructor function
```
1. Determining if a property lives on the prototype
1. 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.
2. `hasOwnProperty()`
1. `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.
```javascript
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.
```
1. Check if an object is an instance of a Class
1. `object instanceof Class`
2. 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.
```javascript
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.