Last active
August 31, 2021 02:33
-
-
Save obrenoco/5da14128c65136b298b49f35fe61c4fe to your computer and use it in GitHub Desktop.
Prototypes and Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functional Instantiation | |
// the weakness of this pattern is that whenever we create | |
// a new person, we have to re-create all the methods in memory, | |
// which is not cool.. | |
function Person (name, energy) { | |
let person = {} | |
person.name = name | |
person.energy = energy | |
person.eat = function (amount) { | |
console.log(`${this.name} is eating !`) | |
this.energy += amount | |
} | |
return person | |
} | |
const breno = Person('Breno', 8) | |
breno.eat(5) // Breno is eating ! | |
--- | |
// Functional Instantiation + Shared Methods | |
// every time we create a new person, we move them to their | |
// own object, then we can have each person reference that object | |
const personMethods = { | |
eat(amount) { | |
console.log(`${this.name} is eating !`) | |
this.energy += amount | |
} | |
} | |
function Person (name, energy) { | |
let person = {} | |
person.name = name | |
person.energy = energy | |
person.eat = personMethods.eat | |
return person | |
} | |
const rodrigo = Person('Rodrigo', 7) | |
rodrigo.eat(5) // Rodrigo is eating ! | |
--- | |
// | |
// Prototypal Instantiation | |
function Person (name, energy) { | |
let person = Object.create(Person.prototype) | |
person.name = name | |
person.energy = energy | |
return person | |
} | |
Person.prototype.eat = function (amount) { | |
console.log(`${this.name} is eating !`) | |
this.energy += amount | |
} | |
const leo = Person('Leo', 5) | |
leo.eat(5) // Leo is eating ! | |
// Object.Create allows to create an object and whenever there's a failed property | |
// on that object, it can consult another object to see if that other | |
// object has that property. | |
// | |
// ex.: | |
// const parent = { | |
// name: 'Ana', | |
// nationality: 'Brazilian' | |
// } | |
// | |
// const child = Object.create(parent) | |
// child.name = 'Matheus' | |
// | |
// console.log(child.name) // Matheus | |
// console.log(child.nationality) // Brazilian | |
--- | |
// Prototypes with 'new' keyword | |
function PersonWithNew (name, energy) { | |
// under the hood, when the 'new' keyword is added, | |
// it runs Object.create() ans return it. | |
this.name = name | |
this.energy = energy | |
} | |
PersonWithNew.prototype.eat = function (amount) { | |
console.log(`${this.name} is eating !`) | |
this.energy += amount | |
} | |
const allan = new PersonWithNew('Allan', 9) | |
allan.eat(5) // Allan is eating ! | |
--- | |
// Classes (ES6) | |
// in JavaScript, classes are just 'sugar sintax' of prototypes | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes | |
class PersonClass { | |
constructor(name, energy) { | |
this.name = name | |
this.energy = energy | |
} | |
eat() { | |
console.log(`${this.name} is eating !`) | |
this.energy += amount | |
} | |
} | |
const igor = new PersonClass('Igor', 2) | |
igor.eat(5) // Igor is eating ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment